android怎么来判断蓝牙开、关的状态?求代码

if(bluetoothAdapter!=null&&bluetoothAdapter.isEnabled()){这里能判断蓝牙开启的状态blue_name=blueto... if (bluetoothAdapter!=null&&bluetoothAdapter.isEnabled()) { 这里能判断蓝牙开启的状态 blue_name=bluetoothAdapter.getName(); bluetooth.setText("蓝牙名称:"+blue_name); bluetooth_switch.setChecked(true); isBluetooth=true; } else if(bluetoothAdapter==null){ bluetooth.setText("蓝牙名称:"); isBluetooth=false; bluetooth_switch.setChecked(false); Toast.makeText(this, "此设备不支持蓝牙", Toast.LENGTH_SHORT).show(); } else if(????????????_){这里需要用什么来判断蓝牙已经关闭的状态? } 展开
 我来答
QQMSD8
2015-09-19 · 知道合伙人软件行家
QQMSD8
知道合伙人软件行家
采纳数:6788 获赞数:13239
没有做不到,只有想不到,帮助别人的同时也是对自己的提升

向TA提问 私信TA
展开全部

Android 蓝牙编程的基本步骤:

  1.  获取蓝牙适配器BluetoothAdapter blueadapter=BluetoothAdapter.getDefaultAdapter(); 

  2. 如果BluetoothAdapter 为null,说明android手机没有蓝牙模块。

    判断蓝牙模块是否开启,blueadapter.isEnabled() true表示已经开启,false表示蓝牙并没启用。

  3. 启动配置蓝牙可见模式,即进入可配对模式Intent in=new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);  

    in.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 200);  

    startActivity(in);  ,200就表示200秒。

  4. 获取蓝牙适配器中已经配对的设备Set<BluetoothDevice> device=blueadapter.getBondedDevices(); 

  5. 还需要在androidManifest.xml中声明蓝牙的权限

 <uses-permission android:name="android.permission.BLUETOOTH" />

 <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />  


接下来就是根据自己的需求对BluetoothAdapter 的操作了。

网易云信
2023-12-06 广告
信令SDK是一种软件开发工具包,旨在帮助开发者在应用程序中实现信令协议的通信功能。它主要提供了一系列函数、协议和工具,用于处理信令消息的生成、解析、传输和存储等操作。通过使用信令SDK,开发者可以更快速、便捷地实现信令通信功能,提高应用程序... 点击进入详情页
本回答由网易云信提供
摩朝雨0iOe99
2014-06-21 · 超过69用户采纳过TA的回答
知道答主
回答量:129
采纳率:25%
帮助的人:67.1万
展开全部
可以分开判断,bluetoothAdapter!=null 代表本机有蓝牙设备,接着再去判断 isEnabled() 则可以确定是否是开启状态了。 查看原帖>>
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
wangwei7066
2015-09-04 · TA获得超过1.9万个赞
知道大有可为答主
回答量:6693
采纳率:54%
帮助的人:1295万
展开全部
android中可以通过以下代码监听蓝牙的开关状态:

BluetoothService.java文件

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.UUID;

import com.dvb.detector.constant.Constant;
import com.dvb.detector.util.StringUtil;
import com.dvb.detector.activity.DeviceConnectActivity;

import android.app.Service;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;
/**
*
*蓝牙服务
*/
public class BluetoothService extends Service {
private static final String NAME_SECURE = "BluetoothSecure";

private static final UUID MY_UUID_SECURE = UUID
.fromString("00001101-0000-1000-8000-00805F9B34FB");

private BluetoothAdapter mAdapter;
/**
* 正在连接的线程,连接成功以后切至ConnectedThread
*/
private ConnectThread mConnectThread;
/**
* 用于连接后的读写操作线程
*/
private ConnectedThread mConnectedThread;
/**
* 当前连接的状态值,分别为以下三个状态值
*/
private int mState;

// 当前连接状态
/**
* 无连接
*/
public static final int STATE_NONE = 0;
/**
* 正在连接
*/
public static final int STATE_CONNECTING = 2;
/**
* 连接成功
*/
public static final int STATE_CONNECTED = 3;

private final IBinder binder = new BluetoothBinder();
/**
* 单片机mac地址
*/
private String address;
/**
* 重连次数设定
*/
private int reConnectCount;
/**
* 最大重连次数
*/
private static final int MAX_RECONNECT_TIMES = 2;

public class BluetoothBinder extends Binder {
public BluetoothService getService() {
return BluetoothService.this;
}
}

@Override
public IBinder onBind(Intent intent) {
return binder;
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
mAdapter = BluetoothAdapter.getDefaultAdapter();
connectDevice(intent);
mState = STATE_NONE;
return START_REDELIVER_INTENT;
}

/**
* 连接机器
*
* @param data
*/
private void connectDevice(Intent data) {
address = data.getStringExtra("MAC");
// 根据Mac地址获取device
BluetoothDevice device = mAdapter.getRemoteDevice(address);
// 尝试连接该机器
connect(device);
}
/**
* 发起重连
*/
private void reConnectDevice() {
/**
* 最大重连次数之内直接重连,否则跳回至设备选择界面
*/
if (reConnectCount <= MAX_RECONNECT_TIMES) {
// Toast.makeText(this, "正在进行蓝牙重连", Toast.LENGTH_LONG).show();
// 根据Mac地址获取device
BluetoothDevice device = mAdapter.getRemoteDevice(address);
// 尝试连接该机器
connect(device);
reConnectCount++;
} else {
Intent intent = new Intent(this, DeviceConnectActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra("flag", 1);
/* startActivity(intent);*/
}
}

/**
* 和机器连接
*/
public synchronized void connect(BluetoothDevice device) {
if (mState == STATE_CONNECTING) {
if (mConnectThread != null) {
mConnectThread.cancel();
mConnectThread = null;
}
}

if (mConnectedThread != null) {
mConnectedThread.cancel();
mConnectedThread = null;
}
mConnectThread = new ConnectThread(device);
mConnectThread.start();
setState(STATE_CONNECTING);
}

/**
* 连接上机器,开启连接对话管理线程
*/
public synchronized void connected(BluetoothSocket socket, BluetoothDevice device) {
if (mConnectThread != null) {
mConnectThread.cancel();
mConnectThread = null;
}

if (mConnectedThread != null) {
mConnectedThread.cancel();
mConnectedThread = null;
}
// 开启连接对话管理线程
mConnectedThread = new ConnectedThread(socket);
mConnectedThread.start();
reConnectCount = 0;
setState(STATE_CONNECTED);
}

/**
* 停止所有线程
*/
public synchronized void stop() {
if (mConnectThread != null) {
mConnectThread.cancel();
mConnectThread = null;
}

if (mConnectedThread != null) {
mConnectedThread.cancel();
mConnectedThread = null;
}

setState(STATE_NONE);
}

/**
* 异步发送数据
*/
public void write(byte[] out) {
ConnectedThread r;
synchronized (this) {
if (mState != STATE_CONNECTED)
return;
r = mConnectedThread;
}
r.write(out);
}

/**
* 连接失败
*/
private void connectionFailed() {
BluetoothService.this.reConnectDevice();
Toast.makeText(this,"蓝牙断开正在重新连接", Toast.LENGTH_LONG).show();
}

/**
* 连接丢失
*/
private void connectionLost() {
BluetoothService.this.reConnectDevice();
}

private synchronized void setState(int state) {
Log.i("xth", "state = " + state);
mState = state;
}

public synchronized int getState() {
return mState;
}

/**
* 连接线程
*/
private class ConnectThread extends Thread {
private final BluetoothSocket mmSocket;

private final BluetoothDevice mmDevice;

public ConnectThread(BluetoothDevice device) {
mmDevice = device;
BluetoothSocket tmp = null;
try {
tmp = device.createRfcommSocketToServiceRecord(MY_UUID_SECURE);
} catch (IOException e) {
}
mmSocket = tmp;
}

public void run() {
// 连接前把耗费资源的搜索功能关闭
mAdapter.cancelDiscovery();

try {
mmSocket.connect();
} catch (IOException e) {
try {
mmSocket.close();
} catch (IOException e2) {
}
connectionFailed();
return;
}

// 完成连接,重置连接线程
synchronized (BluetoothService.this) {
mConnectThread = null;
}

connected(mmSocket, mmDevice);
}

public void cancel() {
try {
mmSocket.close();
} catch (IOException e) {
}
}
}

/**
* 已完成配对,处理所有读写消息
*/
private class ConnectedThread extends Thread {
private final BluetoothSocket mmSocket;

private final InputStream mmInStream;

private final OutputStream mmOutStream;

public ConnectedThread(BluetoothSocket socket) {
mmSocket = socket;
InputStream tmpIn = null;
OutputStream tmpOut = null;

try {
tmpIn = socket.getInputStream();
tmpOut = socket.getOutputStream();
} catch (IOException e) {
}

mmInStream = tmpIn;
mmOutStream = tmpOut;
}

public void run() {
byte[] buffer = new byte[Constant.BYTES];
int bytes;

byte[] result = new byte[Constant.BYTES];
int resultLenth = 0;
// 持续监听是否收到消息
while (true) {
try {
//每次接收到20个字节则转换成本地数据结构FPItem存储
bytes = mmInStream.read(buffer);
if (resultLenth < Constant.BYTES && bytes + resultLenth <= Constant.BYTES) {
System.arraycopy(buffer, 0, result, resultLenth, bytes);
resultLenth += bytes;
}

if (resultLenth >= Constant.BYTES) {
resultLenth = 0;
StringUtil.list.add(result);
}
} catch (Exception e) {
connectionLost();
break;
}
}
}

/**
* 写数据,向单片机输出数据
*/
public void write(byte[] buffer) {
try {
mmOutStream.write(buffer);
} catch (IOException e) {
}
}

public void cancel() {
try {
mmSocket.close();
} catch (IOException e) {
}
}
}
}
DeviceConnectThread.java文件

import android.app.Activity;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

import com.dvb.detector.constant.Constant;
import com.dvb.detector.service.BluetoothService;
import com.dvb.detector.util.StringUtil;
/**
* 设备连接线程,向单片机发起连接请求
* 我方发送474消息,当接收到单片机返回的“lock=3”消息,表明连接成功
*
*/
public class DeviceConnectThread extends Thread {

private Handler handler;

private BluetoothService bluetoothService;

private int readCount;

private boolean stop;

public DeviceConnectThread(Activity activity, Handler handler, BluetoothService bluetoothService) {
this.handler = handler;
this.bluetoothService = bluetoothService;
}

@Override
public void run() {
for (int i = 0; i < Constant.DEVICE_ACTIVITY_LIMIT; i++) {
bluetoothService.write(StringUtil.getSendData("474"));
readCount = 0;
Log.i("xth","Wait write ");
while (readCount <= Constant.DEVICE_ACTIVITY_EACH_LIMIT) {
while (!StringUtil.list.isEmpty()) {
Log.i("xth","notEmpty");
Message msg = handler.obtainMessage();
msg.obj = StringUtil.list.poll();
msg.what = Constant.CONNECT;
handler.sendMessage(msg);
readCount = Constant.DEVICE_ACTIVITY_EACH_LIMIT + 1;
}
try {
sleep(Constant.DEVICE_ACTIVITY_SLEEP_TIME);
} catch (InterruptedException e) {
e.printStackTrace();
}
if (readCount++ > Constant.DEVICE_ACTIVITY_EACH_LIMIT) {
return;
}
}
}

if (!stop) {
Message msg1 = handler.obtainMessage();
msg1.what = Constant.CONNECT_FAIL;
handler.sendMessage(msg1);
}
}

public void cancel() {
stop = true;
}
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(1)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式