Android蓝牙耳机连接

use*_*915 1 android bluetooth

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

Gil*_*mov 10

这是可能的,但有时不那么简单.

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

bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (bluetoothAdapter==null) {
   // device not support BT
}
Run Code Online (Sandbox Code Playgroud)

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

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

boolean isEnabled = bluetoothAdapter.isEnabled(); // Equivalent to: getBluetoothState() == STATE_ON
Run Code Online (Sandbox Code Playgroud)

并且,如果未启用,则允许用户通过触发ACTION_REQUEST_ENABLE意图将其打开:

Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, ENABLE_BT_CODE);
Run Code Online (Sandbox Code Playgroud)

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

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

for (BluetoothDevice device: pairedDevices) {
    if (device is the one we look for) {
       return device;
    }
}
Run Code Online (Sandbox Code Playgroud)

如果不是 - 您将需要发出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);
Run Code Online (Sandbox Code Playgroud)

然后按照启动命令执行:

boolean started = bluetoothAdapter.startDiscovery(); //async call!
if (!started) {
   // log error
}
Run Code Online (Sandbox Code Playgroud)

找到设备后,您将需要创建专用的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
}
Run Code Online (Sandbox Code Playgroud)

接下来是connect命令:

   clientSocket.connect(context);
Run Code Online (Sandbox Code Playgroud)

一旦连接返回,您就可以像使用套接字一样来回传输数据,完成后:

  clientSocket.close(context);
Run Code Online (Sandbox Code Playgroud)

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

您将使用不同的套接字生成方法来实现安全与不安全的BT模式.您将使用不同的方法来询问设备以获取支持的UUID.您有时也可能不得不求助于反射来激活隐藏的服务,例如Android <ver 15的getUuids().然后列表继续.

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

我最喜欢的(我有偏见,我写了..)是BTWiz,它将封装上述流程,并为您提供一个简单的异步IO接口.随意尝试一下.