安卓蓝牙启用

3

我正在开发一个蓝牙聊天应用程序。问题在于当我启用蓝牙时,应用程序会启用蓝牙但会导致强制关闭。下一次我启动相同的应用程序(已启用蓝牙),它可以正常运行!我搜索了一些信息,只得到了一些信息,说当我启动启用蓝牙的意图时,代码继续执行而不等待Intent的结果。

        public void run() {

        // 1. Check if Bluetooth is Enabled
        if (!blue.isEnabled()) {
            Intent enable_Bluetooth = new Intent(
                    BluetoothAdapter.ACTION_REQUEST_ENABLE);
            startActivityForResult(enable_Bluetooth, 1);

        }

        // 2. Start Bluetooth Server
        try {
            Server = blue.listenUsingRfcommWithServiceRecord("dhiraj",
                    MY_UUID);

Dhiraj,Android自带一个蓝牙聊天应用程序示例。在实现你的应用程序之前,你应该仔细研究它。请参考此链接中的连接部分,并分析你可能出错的地方:http://developer.android.com/resources/samples/BluetoothChat/src/com/example/android/BluetoothChat/BluetoothChat.html - Mudassir
1个回答

16

首先:

在您的应用程序清单文件中声明蓝牙权限。例如:

<manifest ... >
<uses-permission android:name="android.permission.BLUETOOTH" />
...
</manifest>

设置蓝牙:

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

启用蓝牙:

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

查找设备:

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());
}
}

发现设备:

// Create a BroadcastReceiver for ACTION_FOUND
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
@Override 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

启用发现功能

Intent discoverableIntent = new
Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);
startActivity(discoverableIntent);

我不知道startActivityForResult()的第二个参数'int'是什么意思? - Dhiraj Tayade
找到了错误!其真正发生的原因是背景代码在蓝牙启动之前就调用了Bluetooth ServerSocket.accept(),我只需要加一个循环while(!running){//等待蓝牙启用},问题就解决了……没有强制关闭。 - Dhiraj Tayade
在启用蓝牙时,startActivityForResult()的第二个参数未被接受。我需要添加其他声明吗?..按照TutorialsPoint上的教程,我尝试将0作为第二个参数传递。没有显示错误,但在我的手机上运行时,程序无法启动。 - 20B2

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接