基于SnapDragonBoard410c的蓝牙控制

在你的应用程序能够使用蓝牙进行通信之前,你需要进行确认蓝牙设备是否被当前设备所支持。如果当前设备支持蓝牙,则需要请求开启蓝牙设备。该部分可使用BluetoothAdapter通过两步完成。

(1)BluetoothAdapter对于任何的蓝牙行为都是必备的。获取BluetoothAdapter,可通过调用静态方法getDefaultAdater()。该方法返回一个BluetoothAdapter对象,用来代表自己设备的蓝牙适配器。如果getDefaultAdapter()返回null,那么该设备并不支持蓝牙.

BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (mBluetoothAdapter == null) {
    // Device does not support Bluetooth
}

(2)接下来,你需要确保蓝牙处于开启的状态。调用isEnabled()方法来检查蓝牙目前是否可用。如果该方法返回false,那么蓝牙处于不可用的状态。为了请求蓝牙设备的开启,使用ACTION_REQUEST_ENABLE的Intent,并调用startActivityForResult()方法。这将会通过系统设置开启你的蓝牙,例如:

if (!mBluetoothAdapter.isEnabled()) {
    Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
    startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}

调用该方法后,一个请求开启蓝牙的对话框将会出现在屏幕上。如果用户点击确认,那么系统将会开启蓝牙设备,该过程完成(或失败)后,将会回到你的应用程序。

使用BluetoothAdapter,通过搜索设备或查询配对设备列表可以找到远程蓝牙设备。

设备搜索(Device Discovery)是一个扫描的过程,用来搜索本地开启蓝牙的设备,在此之后请求每一个扫描到设备的信息。然而,一个蓝牙设备只有处于可见状态下才会返回设备信息,例如设备名称,MAC地址等。使用该信息,设备能够实例化和该设备的蓝牙连接。

当第一次和远程蓝牙设备进行连接时,一个配对的请求将会自动呈现在用户面前。当设备配对时,设备的基础信息将会被保存并能够使用蓝牙的API进行读取。使用远程蓝牙设备的MAC地址,介于蓝牙设备间的连接将能够在任意时刻实例化,而不需要进行搜索操作(假定设备在蓝牙的通信范围内)

(3)查询配对设备

在搜索设备之前,有必要查询已配对的设备集,来得知想要连接的设备是否已经配对。为了执行上述操作,可以调用getBondedDevices()方法。该方法返回一个BluetoothDevice的集合来代表配对设备。例如,你可以查询所有的配对设备并使用ArrarAdapter显示它们:

Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
// If there are paired devices
if (pairedDevices.size() > 0) {
    // Loop through paired devices
    for (BluetoothDevice device : pairedDevices) {
        // Add the name and address to an array adapter to show in a ListView
        mArrayAdapter.add(device.getName() + "\n" + device.getAddress());
    }
}

需要使用BluetoothDevice进行连接实例化的唯一值是MAC地址。在上述的例子中,该部分作为ArrayAdapter的一部分呈现给用户。关于如何使用MAC地址实例化连接,请参考后文。

(4)发现设备

执行发现设备的操作,仅仅需要执行startDiscovery()方法。该过程是异步的,该方法将会立刻返回一个布尔值表明搜索是否已经开始。通常情况下,该搜索的过程调用12秒钟的查询,随后返回找到的设备。

你的应用程序必须使用ACTION_FOUNDd的Intent注册一个BroadastReceiver。该Intent用来接受每一个查找到设备的信息。对于每一个设备,系统将会广播ACTION_FOUND。该Intent包含两个额外域,EXTRA_DEVICE 和 EXTRA_CLASS。分别包含一个BluetoothDevice类对象和BluetoothClass类对象。例如:

// Create a BroadcastReceiver for ACTION_FOUND
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        // When discovery finds a device
        if (BluetoothDevice.ACTION_FOUND.equals(action)) {
            // Get the BluetoothDevice object from the Intent
            BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            // Add the name and address to an array adapter to show in a ListView
            mArrayAdapter.add(device.getName() + "\n" + device.getAddress());
        }
    }
};
// Register the BroadcastReceiver
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver(mReceiver, filter); // Don't forget to unregister during onDestroy

执行设备搜索的操作是一项很繁重的任务,会消耗大量的资源。一旦你找到了一个设备并要进行连接,请务必确认是否停止搜索设备的操作。如果已经进行了连接,那么搜索操作将会显著地降低连接的速率,因此你应当在连接时停止搜索。可通过cancelDiscovery()方法停止搜索。

猜你喜欢

转载自blog.csdn.net/u013763766/article/details/79214636