Android蓝牙耳机连接

时间:2014-06-28 05:24:16

标签: android bluetooth

我是Android平台的新手。我正在使用一个应用程序需要集成蓝牙。该要求不是手动连接和断开蓝牙耳机(HSP配置文件),应该可以在应用程序内连接和断开连接。是否可以在运行OS 4.2,4.3和4.4的Android设备中连接和断开设备。如果有任何人有这个问题的解决方案,请告诉我相同的。

1 个答案:

答案 0 :(得分:9)

有可能,但有时不那么简单。

要进行连接,首先要检查您运行的设备是否完全支持BT:

bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (bluetoothAdapter==null) {
   // device not support BT
}

如果没有 - 优雅地禁用应用的BT部分并继续前进。

如果支持,请检查天气是否已启用(请记住 - 用户可以 打开BT&和其他沟通渠道一样):

boolean isEnabled = bluetoothAdapter.isEnabled(); // Equivalent to: getBluetoothState() == STATE_ON

并且,如果没有启用,允许用户通过触发ACTION_REQUEST_ENABLE意图打开它:

Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, ENABLE_BT_CODE);

一旦您了解可用性,请执行针对您所针对的特定设备的查找。 从Android维护的绑定设备列表开始总是一个好主意:

Set<BluetoothDevice> bondedDevices = bluetoothAdapter.getBondedDevices();

for (BluetoothDevice device: pairedDevices) {
    if (device is the one we look for) {
       return device;
    }
}

如果不是 - 您将需要发出BT发现命令。

必须永远不要在UI线程上执行发现,所以请生成一个线程(使用AsyncTask,Executer ......) 做这项工作。

当BT连接操作仍在进行时,不应执行发现。该 对设备资源的影响太大。

首先设置发现接收器:

discoveryReceiver = new BroadcastReceiver() {
    private boolean wasFound = false;
    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        System.out.println(action);
        if (BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action)) {
            discoveryStatus = STARTED;
        }
        else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
            discoveryStatus = FINISHED;
        }
        else if (BluetoothDevice.ACTION_FOUND.equals(action)) {
            BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            if (device is what we look for) {
                stopDiscovery(context);
            }
        }
    }
};
IntentFilter filter = new IntentFilter();
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
filter.addAction(BluetoothDevice.ACTION_FOUND);
context.registerReceiver(discoveryReceiver, filter);

然后按照开始命令执行:

boolean started = bluetoothAdapter.startDiscovery(); //async call!
if (!started) {
   // log error
}

找到设备后,您需要创建一个专用的BT插座:

BluetoothSocket clientSocket = null;
try {
    if (secureMode == SECURE) {
        clientSocket = device.createRfcommSocketToServiceRecord(serviceUuid);
    }
    else { // INSECURE
        clientSocket = device.createInsecureRfcommSocketToServiceRecord(serviceUuid);
    }
    if (clientSocket == null) {
       throw new IOException();
    }

} catch (IOException e) {
    // log error
}

接下来是connect命令:

   clientSocket.connect(context);

一旦连接返回,您就可以传回数据&amp;你使用套接字的方式, 完成后:

  clientSocket.close(context);

以上描述了一般流程。在许多情况下,你的工作会更难:

您将使用不同的套接字生成方法来实现安全与不安全的BT模式。你将使用不同的 询问设备支持的UUID的方法。您有时也可能不得不求助于反射来激活隐藏的服务,例如适用于Android的getUuids()&lt;第15页。列表继续。

对于初学者来说,使用工具来完成这项工作是有道理的。

我最喜欢的(我有偏见,我写了..)是BTWiz,它将封装上述内容 来自您的流程,还将为您提供异步IO的简单界面。随意尝试一下。

相关问题