Android 接收BLE藍(lán)牙廣播 ,無連接的情況
直接給出代碼,直接丟進(jìn)去用就好
?/**
? ? ?* --------------------------------------------------------
? ? ?* 藍(lán)牙廣播部分
? ? ?*/
? ? 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;
? ? /**
? ? ?* 直接調(diào)用這個方法就好
? ? ?*/
? ? private void startBroadcast() {
? ? ? ? //初始化ble管理器
? ? ? ? if (bleManager == null) {
? ? ? ? ? ? bleManager = new BLEManager();
? ? ? ? }
? ? ? ? if (!bleManager.initBle(this)) {
? ? ? ? ? ? Log.d(TAG, "該設(shè)備不支持低功耗藍(lán)牙");
? ? ? ? ? ? Toast.makeText(this, "該設(shè)備不支持低功耗藍(lán)牙", Toast.LENGTH_SHORT).show();
? ? ? ? } else {
? ? ? ? ? ? if (!bleManager.isEnable()) {
? ? ? ? ? ? ? ? //去打開藍(lán)牙
? ? ? ? ? ? ? ? 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:? //掃描到設(shè)備? 接收到廣播也是在這里
? ? ? ? ? ? ? ? ? ? BLEDevice bleDevice = (BLEDevice) msg.obj;
//? ? ? ? ? ? ? ? ? ? lvDevicesAdapter.addDevice(bleDevice);
//? ? ? ? ? ? ? ? ? ? Log.e("Link", "接收廣播數(shù)據(jù)數(shù)據(jù):" + bleDevice.getBluetoothDevice().getName());
? ? ? ? ? ? ? ? ? ? /**
? ? ? ? ? ? ? ? ? ? ?* 這里有廣播接收到的數(shù)據(jù)
? ? ? ? ? ? ? ? ? ? ?*
? ? ? ? ? ? ? ? ? ? ?* 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:? //接收成功? 在這里處理接收到的數(shù)據(jù)
? ? ? ? ? ? ? ? ? ? byte[] recBufSuc = (byte[]) msg.obj;
? ? ? ? ? ? ? ? ? ? String receiveResult = TypeConversion.bytes2HexString(recBufSuc, recBufSuc.length);
? ? ? ? ? ? ? ? ? ? Log.e("Link", "接收數(shù)據(jù)成功,長度:" + recBufSuc.length + "-->" + receiveResult);
? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? case CONNECT_SUCCESS:? //連接成功
? ? ? ? ? ? ? ? ? ? Log.e("B_ActivityFFFFF", "" + "連接成功");
? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? case SEND_SUCCESS:? //發(fā)送成功
? ? ? ? ? ? ? ? ? ? byte[] sendBufSuc = (byte[]) msg.obj;
? ? ? ? ? ? ? ? ? ? String sendResult = TypeConversion.bytes2HexString(sendBufSuc, sendBufSuc.length);
//? ? ? ? ? ? ? ? ? ? Log.e("Link", "發(fā)送數(shù)據(jù)成功,長度:" + sendBufSuc.length + "-->" + sendResult);
? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? case CONNECT_FAILURE: //連接失敗
? ? ? ? ? ? ? ? ? ? Toast.makeText(A_index_Activity.this, "連接失敗", Toast.LENGTH_SHORT).show();
? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? case RECEIVE_FAILURE: //接收數(shù)據(jù)失敗
? ? ? ? ? ? ? ? ? ? String receiveError = (String) msg.obj;
? ? ? ? ? ? ? ? ? ? Log.e("Link", "接收數(shù)據(jù)失敗:" + receiveError);
? ? ? ? ? ? ? ? ? ? Toast.makeText(A_index_Activity.this, "通訊異常", Toast.LENGTH_SHORT).show();
? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? case DISCONNECT_SUCCESS:
? ? ? ? ? ? ? ? ? ? Log.e(TAG, "斷開成功");
? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? case SEND_FAILURE: //發(fā)送失敗
? ? ? ? ? ? ? ? ? ? byte[] sendBufFail = (byte[]) msg.obj;
? ? ? ? ? ? ? ? ? ? String sendFail = TypeConversion.bytes2HexString(sendBufFail, sendBufFail.length);
//? ? ? ? ? ? ? ? ? ? tvSendResult.setText("發(fā)送數(shù)據(jù)失敗,長度" + sendBufFail.length + "--> " + sendFail);
? ? ? ? ? ? ? ? ? ? Log.e(TAG, "發(fā)送數(shù)據(jù)失敗,長度" + sendBufFail.length + "--> " + sendFail);
? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? case START_DISCOVERY:
? ? ? ? ? ? ? ? ? ? Log.d(TAG, "開始搜索設(shè)備...");
? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? case STOP_DISCOVERY:
? ? ? ? ? ? ? ? ? ? Log.d(TAG, "停止搜索設(shè)備...");
? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? case BT_CLOSED:
? ? ? ? ? ? ? ? ? ? Log.e(TAG, "系統(tǒng)藍(lán)牙已關(guān)閉");
? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? case BT_OPENED:
? ? ? ? ? ? ? ? ? ? Log.e(TAG, "系統(tǒng)藍(lán)牙已打開");
? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? }
? ? ? ? }
? ? };
? ? //掃描結(jié)果回調(diào) 廣播也是這里轉(zhuǎn)接
? ? 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);
? ? ? ? }
? ? };