Android 接收BLE蓝牙广播 ,无连接的情况
直接给出代码,直接丢进去用就好
/**
* --------------------------------------------------------
* 蓝牙广播部分
*/
private static final int CONNECT_SUCCESS = 0x01;
private static final int CONNECT_FAILURE = 0x02;
private static final int DISCONNECT_SUCCESS = 0x03;
private static final int SEND_SUCCESS = 0x04;
private static final int SEND_FAILURE = 0x05;
private static final int RECEIVE_SUCCESS = 0x06;
private static final int RECEIVE_FAILURE = 0x07;
private static final int START_DISCOVERY = 0x08;
private static final int STOP_DISCOVERY = 0x09;
private static final int DISCOVERY_DEVICE = 0x0A;
private static final int DISCOVERY_OUT_TIME = 0x0B;
private static final int SELECT_DEVICE = 0x0C;
private static final int BT_OPENED = 0x0D;
private static final int BT_CLOSED = 0x0E;
private static final String TAG = "BLEMain";
private BLEManager bleManager;
/**
* 直接调用这个方法就好
*/
private void startBroadcast() {
//初始化ble管理器
if (bleManager == null) {
bleManager = new BLEManager();
}
if (!bleManager.initBle(this)) {
Log.d(TAG, "该设备不支持低功耗蓝牙");
Toast.makeText(this, "该设备不支持低功耗蓝牙", Toast.LENGTH_SHORT).show();
} else {
if (!bleManager.isEnable()) {
//去打开蓝牙
bleManager.openBluetooth(this, false);
} else {
bleManager.startDiscoveryDevice(onDeviceSearchListener, 850000);//开启扫描接收 广播要用到
}
}
}
private Handler mHandler = new Handler() {
@SuppressLint("SetTextI18n")
@Override
public void handleMessage(@NonNull Message msg) {
super.handleMessage(msg);
switch (msg.what) {
case DISCOVERY_DEVICE: //扫描到设备 接收到广播也是在这里
BLEDevice bleDevice = (BLEDevice) msg.obj;
// lvDevicesAdapter.addDevice(bleDevice);
// Log.e("Link", "接收广播数据数据:" + bleDevice.getBluetoothDevice().getName());
/**
* 这里有广播接收到的数据
*
* ID 10CC
*/
BluetoothDevice bluetoothDevice = bleDevice.getBluetoothDevice();
String Addres = bluetoothDevice.getAddress();
if (Addres.equals("00:80:E1:21:04:10")) {//暂时这样写
Log.e("Link", "getName:" + bluetoothDevice.getName());
// Log.e("Link", "getAddress:" + bluetoothDevice.getAddress());
// Log.e("Link", "getType:" + bluetoothDevice.getType());
// Log.e("Link", "getBondState:" + bluetoothDevice.getBondState());
// Log.e("Link", "getBluetoothClass:" + bluetoothDevice.getBluetoothClass());
// Log.e("Link", "getUuids:" + bluetoothDevice.getUuids());
//
// Log.e("Link", "bluetoothDevice:" + new Gson().toJson(bluetoothDevice));
byte[] adv_data = bleDevice.getBytes();
// String bytes2HexString = TypeConversion.bytes2HexString(bytes);
ByteBuffer buffer = ByteBuffer.wrap(adv_data).order(ByteOrder.LITTLE_ENDIAN);
// Log.e("Link", "remaining:" + TypeConversion.bytes2HexString(buffer.array())); //好像是这个了
// Log.e("Link", "remaining:" + new Gson().toJson(buffer.get()));
//
// Log.e("Link", "getShort:" + buffer.getShort());
// Log.e("Link", "array:" + new Gson().toJson(buffer.compact()));
// Log.e("Link", "array:" + new Gson().toJson(buffer.asCharBuffer()));
// Log.e("Link", "array:" + new Gson().toJson(buffer.asShortBuffer()));
}
break;
case RECEIVE_SUCCESS: //接收成功 在这里处理接收到的数据
byte[] recBufSuc = (byte[]) msg.obj;
String receiveResult = TypeConversion.bytes2HexString(recBufSuc, recBufSuc.length);
Log.e("Link", "接收数据成功,长度:" + recBufSuc.length + "-->" + receiveResult);
break;
case CONNECT_SUCCESS: //连接成功
Log.e("B_ActivityFFFFF", "" + "连接成功");
break;
case SEND_SUCCESS: //发送成功
byte[] sendBufSuc = (byte[]) msg.obj;
String sendResult = TypeConversion.bytes2HexString(sendBufSuc, sendBufSuc.length);
// Log.e("Link", "发送数据成功,长度:" + sendBufSuc.length + "-->" + sendResult);
break;
case CONNECT_FAILURE: //连接失败
Toast.makeText(A_index_Activity.this, "连接失败", Toast.LENGTH_SHORT).show();
break;
case RECEIVE_FAILURE: //接收数据失败
String receiveError = (String) msg.obj;
Log.e("Link", "接收数据失败:" + receiveError);
Toast.makeText(A_index_Activity.this, "通讯异常", Toast.LENGTH_SHORT).show();
break;
case DISCONNECT_SUCCESS:
Log.e(TAG, "断开成功");
break;
case SEND_FAILURE: //发送失败
byte[] sendBufFail = (byte[]) msg.obj;
String sendFail = TypeConversion.bytes2HexString(sendBufFail, sendBufFail.length);
// tvSendResult.setText("发送数据失败,长度" + sendBufFail.length + "--> " + sendFail);
Log.e(TAG, "发送数据失败,长度" + sendBufFail.length + "--> " + sendFail);
break;
case START_DISCOVERY:
Log.d(TAG, "开始搜索设备...");
break;
case STOP_DISCOVERY:
Log.d(TAG, "停止搜索设备...");
break;
case BT_CLOSED:
Log.e(TAG, "系统蓝牙已关闭");
break;
case BT_OPENED:
Log.e(TAG, "系统蓝牙已打开");
break;
}
}
};
//扫描结果回调 广播也是这里转接
private OnDeviceSearchListener onDeviceSearchListener = new OnDeviceSearchListener() {
@Override
public void onDeviceFound(BLEDevice bleDevice) {
Message message = new Message();
message.what = DISCOVERY_DEVICE;
message.obj = bleDevice;
mHandler.sendMessage(message);
}
@Override
public void onDiscoveryOutTime() {
Message message = new Message();
message.what = DISCOVERY_OUT_TIME;
mHandler.sendMessage(message);
}
};