ble兼容Android 4 进行的部分优化

print ?

  1. //1.获取蓝牙管理服务及BluetoothAdapter实例

  2. mBluetoothManager = (BluetoothManager)getSystemService(Context.BLUETOOTH_SERVICE);

  3. mBluetoothAdapter = mBluetoothManager.getAdapter();

  4. if (mBluetoothAdapter == null){

  5. return;

  6. }

//1.获取蓝牙管理服务及BluetoothAdapter实例

mBluetoothManager = (BluetoothManager)getSystemService(Context.BLUETOOTH_SERVICE);

mBluetoothAdapter = mBluetoothManager.getAdapter();

if (mBluetoothAdapter == null){

return;

}

2.打开蓝牙

接下来调用BluetoothAdapter的isEnabled()判断蓝牙是否打开,未打开我们可以发送一个action为BluetoothAdapter.ACTION_REQUEST_ENABLE的intent来打开蓝牙。

[java] view plain copy

print ?

  1. //2. 判断蓝牙是否打开,没有打开就打开

  2. if (!mBluetoothAdapter.isEnabled()){

  3. Intent intent = newIntent(BluetoothAdapter.ACTION_REQUEST_ENABLE);

  4. startActivityForResult(intent,REQUEST_ENABLE_BT);

  5. }

//2. 判断蓝牙是否打开,没有打开就打开

if (!mBluetoothAdapter.isEnabled()){

Intent intent = newIntent(BluetoothAdapter.ACTION_REQUEST_ENABLE);

startActivityForResult(intent,REQUEST_ENABLE_BT);

}

这时界面上回弹出一个对话框提示我们打开蓝牙,打开成功后会回调Activity的onActivityResult(int requestCode, int resultCode, Intent data)方法,我们可以在里面提示一些信息。比如提示蓝牙已经打开等。

[java] view plain copy

print ?

  1. @Override

  2. protected void onActivityResult(int requestCode, int resultCode, Intentdata) {

  3. super.onActivityResult(requestCode, resultCode, data);

  4. if (resultCode != RESULT_OK){

  5. return;

  6. }

  7. if (requestCode == REQUEST_ENABLE_BT){

  8. Toast.makeText(this,“蓝牙已开启”,Toast.LENGTH_LONG).show();

  9. }

  10. }

@Override

protected void onActivityResult(int requestCode, int resultCode, Intentdata) {

super.onActivityResult(requestCode, resultCode, data);

if (resultCode != RESULT_OK){

return;

}

if (requestCode == REQUEST_ENABLE_BT){

Toast.makeText(this,“蓝牙已开启”,Toast.LENGTH_LONG).show();

}

}

3.扫描周围的BLE蓝牙设备

扫描BLE蓝牙设备,对于4.3以上的系统,直接调用startLeScan(BluetoothAdapter.LeScanCallbackcallback)即可扫描出BLE设备,在callback中会回调。但是对于5.0以上的系统,android添加了新的API,原有的startLeScan(BluetoothAdapter.LeScanCallback callback)已经被废弃,在5.0以上的系统中是使用BluetoothLeScanner的startScan(ScanCallbackcallback),回调也是ScanCallback了。

[java] view plain copy

print ?

  1. /**

  2. * 扫描Bluetooth LE

  3. * @param enable

  4. */

  5. private void scanBleDevice(boolean enable){

  6. //android 5.0 以前

  7. if (Build.VERSION.SDK_INT < 21){

  8. if (enable){

  9. mHandler.postDelayed(new Runnable() {

  10. @Override

  11. public void run() {

  12. mScaning = false;

  13. mBluetoothAdapter.stopLeScan(mLeScanCallback);

  14. }

  15. },SCAN_SECOND);

  16. mScaning = true;

  17. mBluetoothAdapter.startLeScan(mLeScanCallback);

  18. } else {

  19. mScaning = false;

  20. mBluetoothAdapter.stopLeScan(mLeScanCallback);

  21. }

  22. } else {

  23. scanner = mBluetoothAdapter.getBluetoothLeScanner();

  24. scanner.startScan(mScanCallback);

  25. mHandler.postDelayed(new Runnable() {

  26. @Override

  27. public void run() {

  28. scanner.stopScan(mScanCallback);

  29. }

  30. },SCAN_SECOND);

  31. }

  32. }

/**

  • 扫描Bluetooth LE

  • @param enable

*/

private void scanBleDevice(boolean enable){

//android 5.0 以前

if (Build.VERSION.SDK_INT < 21){

if (enable){

mHandler.postDelayed(new Runnable() {

@Override

public void run() {

mScaning = false;

mBluetoothAdapter.stopLeScan(mLeScanCallback);

}

},SCAN_SECOND);

mScaning = true;

mBluetoothAdapter.startLeScan(mLeScanCallback);

} else {

mScaning = false;

mBluetoothAdapter.stopLeScan(mLeScanCallback);

}

} else {

scanner = mBluetoothAdapter.getBluetoothLeScanner();

scanner.startScan(mScanCallback);

mHandler.postDelayed(new Runnable() {

@Override

public void run() {

scanner.stopScan(mScanCallback);

}

},SCAN_SECOND);

}

}

扫描的回调如下:

[java] view plain copy

print ?

  1. //sacn扫描回调 5.0以上用

  2. private ScanCallback mScanCallback = new ScanCallback() {

  3. @Override

  4. public void onScanResult(int callbackType, ScanResult result) {

  5. BluetoothDevice device =result.getDevice();

  6. if (device != null){

  7. //过滤掉其他设备

  8. if (device.getName() != null&& device.getName().startsWith(“WINPOS”)){

  9. BLEDevice bleDevice = newBLEDevice(device.getName(),device.getAddress());

  10. if(!mBLEDeviceList.contains(bleDevice)){

  11. mBLEDeviceList.add(bleDevice);

  12. showBluetoothLeDevice(bleDevice);

  13. }

  14. }

  15. }

  16. }

  17. @Override

  18. public void onBatchScanResults(List results) {

  19. Log.d(TAG,”onBatchScanResults”);

  20. }

  21. @Override

  22. public void onScanFailed(int errorCode) {

  23. Log.d(TAG,”onScanFailed”);

  24. }

  25. };

  26. //4.3以上

  27. private BluetoothAdapter.LeScanCallback mLeScanCallback = newBluetoothAdapter.LeScanCallback() {

  28. @Override

  29. public void onLeScan(final BluetoothDevice bluetoothDevice, int i,byte[] bytes) {

  30. if (bluetoothDevice != null){

  31. //过滤掉其他设备

  32. if (bluetoothDevice.getName()!= null && bluetoothDevice.getName().startsWith(“WINPOS”)){

  33. BLEDevice bleDevice = newBLEDevice(bluetoothDevice.getName(),bluetoothDevice.getAddress());

  34. if(!mBLEDeviceList.contains(bleDevice)){

  35. mBLEDeviceList.add(bleDevice);

  36. showBluetoothLeDevice(bleDevice);

  37. }

  38. }

  39. }

  40. }

  41. };

//sacn扫描回调 5.0以上用

private ScanCallback mScanCallback = new ScanCallback() {

@Override

public void onScanResult(int callbackType, ScanResult result) {

BluetoothDevice device =result.getDevice();

if (device != null){

//过滤掉其他设备

if (device.getName() != null&& device.getName().startsWith(“WINPOS”)){

BLEDevice bleDevice = newBLEDevice(device.getName(),device.getAddress());

if(!mBLEDeviceList.contains(bleDevice)){

mBLEDeviceList.add(bleDevice);

showBluetoothLeDevice(bleDevice);

}

}

}

}

@Override

public void onBatchScanResults(List results) {

Log.d(TAG,“onBatchScanResults”);

}

@Override

public void onScanFailed(int errorCode) {

Log.d(TAG,“onScanFailed”);

}

};

//4.3以上

private BluetoothAdapter.LeScanCallback mLeScanCallback = newBluetoothAdapter.LeScanCallback() {

@Override

public void onLeScan(final BluetoothDevice bluetoothDevice, int i,byte[] bytes) {

if (bluetoothDevice != null){

//过滤掉其他设备

if (bluetoothDevice.getName()!= null && bluetoothDevice.getName().startsWith(“WINPOS”)){

BLEDevice bleDevice = newBLEDevice(bluetoothDevice.getName(),bluetoothDevice.getAddress());

if(!mBLEDeviceList.contains(bleDevice)){

mBLEDeviceList.add(bleDevice);

showBluetoothLeDevice(bleDevice);

}

}

}

}

};

这里我预先定义了BLEDevice类,用来表示BLE设备。定义如下:

[java] view plain copy

print ?

  1. public class BLEDevice {

  2. private String name;

  3. private String mac;

  4. public BLEDevice(String name, String mac) {

  5. this.name = name;

  6. this.mac = mac;

  7. }

  8. public String getName() {

  9. return name;

  10. }

  11. public void setName(String name) {

  12. this.name = name;

  13. }

  14. public String getMac() {

  15. return mac;

  16. }

  17. public void setMac(String mac) {

  18. this.mac = mac;

  19. }

最后

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数初中级Android工程师,想要提升技能,往往是自己摸索成长,自己不成体系的自学效果低效漫长且无助

因此我收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点!不论你是刚入门Android开发的新手,还是希望在技术上不断提升的资深开发者,这些资料都将为你打开新的学习之门

如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!
要提升技能,往往是自己摸索成长,自己不成体系的自学效果低效漫长且无助**。

因此我收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。

[外链图片转存中…(img-m9NhDQBM-1715503778027)]

[外链图片转存中…(img-MeAqc14p-1715503778030)]

[外链图片转存中…(img-24fa7W7v-1715503778031)]

[外链图片转存中…(img-kIksF8bi-1715503778031)]

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点!不论你是刚入门Android开发的新手,还是希望在技术上不断提升的资深开发者,这些资料都将为你打开新的学习之门

如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!

  • 11
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值