3

アプリケーションを使用して、クライアントの Bluetooth デバイスを電話にペアリングする作業を行っています。現在、asynctask で次のコードを使用して、リモート デバイス用のソケットを作成しようとしています。

try{
    ba.cancelDiscovery();
    socket= blud.createRfcommSocketToServiceRecord(uuid);
    Method m = blud.getClass().getMethod("createRfcommSocket", new                  
    Class[] {int.class});
    socket = (BluetoothSocket) m.invoke(blud, 1);
}catch(Exception e){e.printStackTrace();}

Connect Thread と Accept Thread がある場合の BluetoothChat の例にさらに沿う必要があるかどうか、または asynctasks で十分かどうかはわかりません。

これは私が使用しているUUID文字列です最終的に Permission Denied IO Exception が発生する

4

2 に答える 2

0

Bluetooth のアクセス許可については、Android マニフェスト ファイルに次を追加します。

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

結合を作成するには、これを使用します -

public boolean createBond(BluetoothDevice btDevice)  
    throws Exception  
    { 
        Class class1 = Class.forName("android.bluetooth.BluetoothDevice");
        Method createBondMethod = class1.getMethod("createBond");  
        Boolean returnValue = (Boolean) createBondMethod.invoke(btDevice);  
        return returnValue.booleanValue();  
    } 

ソケットを作成するには -

private BluetoothSocket createBluetoothSocket(BluetoothDevice device) throws IOException {
        if(Build.VERSION.SDK_INT >= 10){
            try {
                final Method  m = device.getClass().getMethod("createInsecureRfcommSocketToServiceRecord", new Class[] { UUID.class });
                return (BluetoothSocket) m.invoke(device, my_UUID);
            } catch (Exception e) {
                Log.e("No Connection Created", "Could not create Insecure RFComm Connection",e);
            }
        }
        return  device.createRfcommSocketToServiceRecord(my_UUID);
    }

Android開発者ドキュメントからBluetoothアダプタも確認してください。

于 2014-03-08T07:04:59.250 に答える
0

こちらの Android API で言及されているように、マニフェストで bluetooth 許可と BLUETOOTH_ADMIN 許可を要求する必要があります。

「注: ほとんどのメソッドには BLUETOOTH 権限が必要ですが、一部のメソッドには BLUETOOTH_ADMIN 権限も必要です。」

于 2012-10-24T22:21:53.150 に答える