Android BLE学习(一): Android搜索BLE设备

 http://my.csdn.net/lidec


背景

总结一下最近ble的学习情况。自从入手ble 51822开发板后就开始不停加班,中途出于好奇,业余时间写了一些单片机上json解析相关的东西,妄图使用蓝牙传输json数据,不知道是否实用,既然开始写了,得写出点样子,晃晃荡荡,2016年的1月份就过去了。

这里本章我们主要总结一下ble搜索相关的内容,先建立直观印象,然后剖析ble模块与Android相关代码,看看源码与现象是如何对应的。最后,当我们了解流程后,就可以比较容易地理解蓝牙协议中的一些内容,最终实现照我们自己的需求建立协议,开发属于我们自己的模块的目的。

51822中自带了蓝牙协议栈,协议栈也规定了程序的框架,感觉这样的好处就是简化了开发流程,我们可以按照能跑通的demo进行修改即可,方便学习。

Android BLE 搜索

BluetoothAdapter

以下是android官方对这个类的介绍

Represents the local device Bluetooth adapter. The BluetoothAdapter lets you perform fundamental Bluetooth tasks, such as initiate device discovery, query a list of bonded (paired) devices, instantiate a BluetoothDevice using a known MAC address, and create a BluetoothServerSocket to listen for connection requests from other devices, and start a scan for Bluetooth LE devices.

To get a BluetoothAdapter representing the local Bluetooth adapter, when running on JELLY_BEAN_MR1 and below, call the static getDefaultAdapter() method; when running on JELLY_BEAN_MR2 and higher, retrieve it through getSystemService(String) with BLUETOOTH_SERVICE. Fundamentally, this is your starting point for all Bluetooth actions. Once you have the local adapter, you can get a set of BluetoothDevice objects representing all paired devices with getBondedDevices(); start device discovery with startDiscovery(); or create a BluetoothServerSocket to listen for incoming connection requests with listenUsingRfcommWithServiceRecord(String, UUID); or start a scan for Bluetooth LE devices with startLeScan(LeScanCallback).

大意在讲这个类代表了本地蓝牙适配器,可以对蓝牙进行一些基本操作,比如: 
1.发现设备 
2.获取配对设备列表 
3.获取设备mac地址 
4.创建监听 
5.搜索BLE设备

我们所需要的就是其搜索BLE设备的功能。这也就是为什么我们的Android程序在运行时只能看到ble设备而看不到其他一些蓝牙设备的原因。

下面看一下我们需要用到搜索ble设备的API

boolean startLeScan(BluetoothAdapter.LeScanCallback callback)

启动搜索BLE设备

boolean startLeScan(UUID[] serviceUuids, BluetoothAdapter.LeScanCallback callback)

搜索指定serviceUUID的BLE设备

void stopLeScan(BluetoothAdapter.LeScanCallback callback)

停止收索BLE设备

其中的回调函数就是收索到设备后进行的回调。

BlueToothAdapter通过BluetoothManager的getAdapter()方法获取实例,具体代码如下:

final BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);

mBluetoothAdapter = bluetoothManager.getAdapter();
 
  
  
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3

搜索流程

开始开启蓝牙设备检查BLE设备是否可用BLE设备可用?开始搜索BLE设备并获取相关信息结束yesno

开启蓝牙设备

这里我们使用一个Intent来开启系统牙,此处我们还需要为应用添加蓝牙相关权限

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

开启蓝牙,返回时接收Result,用于显示蓝牙是否开启成功。

Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableIntent, 1);
 
  
  
  • 1
  • 2
  • 1
  • 2

检查BLE是否可用

private void checkBLEDevice(){
 // Use this check to determine whether BLE is supported on the device.  Then you can
 // selectively disable BLE-related features.
      if(!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) {
            Toast.makeText(this, " ble not supported ", Toast.LENGTH_SHORT).show();
            finish();
}

// Initializes a Bluetooth adapter.  For API level 18 and above, get a reference to
// BluetoothAdapter throughBluetoothManager.
final BluetoothManager bluetoothManager = (BluetoothManager)getSystemService(Context.BLUETOOTH_SERVICE);
        mBluetoothAdapter = bluetoothManager.getAdapter();

// Checks if Bluetooth is supported on the device.
if (mBluetoothAdapter == null) {
        Toast.makeText(this, " ble not supported ", Toast.LENGTH_SHORT).show();
        finish();
        return;
    }
}
 
  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

搜索

mBluetoothAdapter.stopLeScan(mBLEScanCallback);//启动搜索BLE设备

mBluetoothAdapter.startLeScan(mBLEScanCallback);//停止搜索BLE设备
 
  
  
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3

当收索到内容时,将调用我们设置的回调函数。可以得到信号强度和BLE设备信息以及广播内容。

new BluetoothAdapter.LeScanCallback(){
            @Override
            public void onLeScan(final BluetoothDevice device, final int rssi, byte[] scanRecord) {

                Log.i("MainActivity", device.getAddress());
                addBLEDeviceData(device, rssi);

            }
        };
 
  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

VONCHENCHEN_BLE就是我们的51822 BLE设备。

VONCHENCHEN_BLE就是我们的51822 BLE设备

小结

至此,我们就完成了BLE设备的搜索,总结一下就是使用BluetoothAdapter类提供的方法完成对BLE设备的扫描,获取到BLE设备的相关信息,如设备名字和Mac地址等,我们可以使用这些信息进行蓝牙连接。

猜你喜欢

转载自blog.csdn.net/lilifang_2011/article/details/72897341
今日推荐