1.获取BluetoothManager服务及BluetoothAdapter实例

BluetoothManager是系统管理蓝牙的一种服务,获取了BluetoothManager我们就可以通过其getAdapter()获取蓝牙适配器了。

[java] view plain copy

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. };

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

深知大多数初中级安卓工程师,想要提升技能,往往是自己摸索成长,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

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

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频
如果你觉得这些内容对你有帮助,可以添加下面V无偿领取!(备注Android)
img

最后相关架构及资料领取方式:

点击我的GitHub免费领取获取往期Android高级架构资料、源码、笔记、视频。高级UI、性能优化、架构师课程、NDK、混合式开发(ReactNative+Weex)微信小程序、Flutter全方面的Android进阶实践技术,群内还有技术大牛一起讨论交流解决问题。

…(img-VdJQnTMD-1711179273469)]
[外链图片转存中…(img-4IGfJ3tl-1711179273469)]

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频
如果你觉得这些内容对你有帮助,可以添加下面V无偿领取!(备注Android)
[外链图片转存中…(img-ZCbzXdVW-1711179273470)]

最后相关架构及资料领取方式:

点击我的GitHub免费领取获取往期Android高级架构资料、源码、笔记、视频。高级UI、性能优化、架构师课程、NDK、混合式开发(ReactNative+Weex)微信小程序、Flutter全方面的Android进阶实践技术,群内还有技术大牛一起讨论交流解决问题。

Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐