蓝牙Android 4.0低耗开发兼容稳定性问题解决方式

Xtremeprog 11年前

可以在 www.IOTSDK.com  下载一个SDK,解决~!

android4.2以上蓝牙开发稳定兼容Samsung BLE SDK、Broadcom BLE SDK、Google BLE SDK 

1. 拷贝依赖库

将如下 jar 包拷贝到 libs 目录下:

  • blelibrary-0.1.jar
  • com.broadcom.bt.jar
  • commons-codec-1.8.jar
  • samsung_ble_sdk_200.jar

2. 添加权限

在 AndroidManifest.xml 中添加如下权限

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

3. 添加 Service

在 AndroidManifest.xml 中添加如 Service

<application>      ...      <service          android:name="com.xtremeprog.sdk.ble.BleService"          android:enabled="true" />      ...  </application>

4. 绑定 BLE service 并接收广播

在需要使用 BLE 通讯的 Activity 中(或者写在 Application 中),添加 bind service 的代码:

    protected BleService mService;      protected IBle mBle;        ...        private final BroadcastReceiver mBleReceiver = new BroadcastReceiver() {              @Override              public void onReceive(Context context, Intent intent) {                      Bundle extras = intent.getExtras();                      String action = intent.getAction();                        // 4. 处理 service 发过来的广播                        // 发现设备                      if (BleService.BLE_DEVICE_FOUND.equals(action)) {                              BluetoothDevice device = extras                                              .getParcelable(BleService.EXTRA_DEVICE);                              byte[] scanRecord = extras                                              .getByteArray(BleService.EXTRA_SCAN_RECORD);                              return;                      }                        // 设备已连接                      if (BleService.BLE_GATT_CONNECTED.equals(action)) {                              BluetoothDevice device = extras                                              .getParcelable(BleService.EXTRA_DEVICE);                              return;                      }                        // 发现设备服务                      if (BleService.BLE_SERVICE_DISCOVERED.equals(action)) {                              String address = extras                                              .getParcelable(BleService.EXTRA_ADDR);                              ArrayList<BleGattService> services = mBle.getServices(address);                              return;                      }                                           }      };        private final ServiceConnection mServiceConnection = new ServiceConnection() {              @Override              public void onServiceConnected(ComponentName className,                              IBinder rawBinder) {                      // 1. 绑定 service 成功,获取 mBle 对象                      mService = ((BleService.LocalBinder) rawBinder).getService();                      mBle = mService.getBle();              }                @Override              public void onServiceDisconnected(ComponentName classname) {                      mService = null;                      mBle = null;              }      };          @Override      public void onCreate(Bundle savedInstanceState) {                ...                // 0. 绑定 service              Intent bindIntent = new Intent(this, BleService.class);              bindService(bindIntent, mServiceConnection, Context.BIND_AUTO_CREATE);      }          @Override      protected void onResume() {              super.onResume();              // 3. 接受 service 发送的广播              registerReceiver(mBleReceiver, BleService.getIntentFilter());      }        @Override      protected void onPause() {              super.onPause();              // 5. 停止接收广播              unregisterReceiver(mBleReceiver);      }

5. BLE 操作

所有 BLE 的操作都通过调用 IBle 对象的 requestXXX 来实现,调用的结果在广播中返回,

    // 请求连接      mBle.requestConnect(address);        // 读取 characteristic      List<BleGattCharacteristic> gattCharacteristics = gattService.getCharacteristics();      mBle.requestReadCharacteristic(address, gattCharacteristics.get(1));        // 写入 characteristic      public static final UUID CHARACTER_COMMON_WRITE = UUID          .fromString("00008a81-0000-1000-8000-00805f9b34fb");      BleGattCharacteristic characteristic = deviceSerivce              .getCharacteristic(CHARACTER_COMMON_WRITE);       characteristic.setValue(new byte[] {0x00});      mBle.requestWriteCharacteristic(address, characteristic, "");        // 接收 notification      mBle.requestCharacteristicNotification(address, characteristic)