在listenUsingRfcommWithServiceRecord创建一个监听

时间:2014-04-20 12:09:47

标签: android bluetooth android-bluetooth

我正在使用android的Bluetooth API。我在这里使用BluetoothServerSocket&创建客户端 - 服务器连接BluetoothSocket,但我的计划陷入了某一点。

// Create a BroadcastReceiver for ACTION_FOUND
private BroadcastReceiver mReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        // When discovery find a device
        if (BluetoothDevice.ACTION_FOUND.equals(action)) {
            // get the BluetoothDevice object from the Intent
            BluetoothDevice mBluetoothDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            Log.i("MainActivity", "Device Name: " + mBluetoothDevice.getName() + " Address: " + mBluetoothDevice.getAddress());
            new AcceptThread().start();
        }
    }
};

private class AcceptThread extends Thread {
    private BluetoothServerSocket mBluetoothServerSocket ;
    public AcceptThread() {
        try {
            mBluetoothServerSocket = mBluetoothAdapter.listenUsingRfcommWithServiceRecord("BT_SERVER", UUID.fromString("a60f35f0-b93a-11de-8a39-08002009c666"));
        } catch (IOException e) {
            Log.e("MainActivity", e.getMessage());
        }
    }

    @Override
    public void run() {
        BluetoothSocket mBluetoothSocket;
        // Keep listening until exception occurs or a socket is returned
        while(true) {
            try {
                mBluetoothSocket = mBluetoothServerSocket.accept();
            } catch (IOException e) {
                break;
            }

            // If a connection was accepted
            if(mBluetoothSocket != null) {
                // transfer the data here
                Toast.makeText(MainActivity.this, "Socket is created", Toast.LENGTH_LONG).show();;
                try {
                    // close the connection to stop to listen any connection now
                    mBluetoothSocket.close();
                } catch(IOException e) { }
            }
        }
    }
}

我的程序卡住了

mBluetoothServerSocket = mBluetoothAdapter.listenUsingRfcommWithServiceRecord("BT_SERVER", UUID.fromString("a60f35f0-b93a-11de-8a39-08002009c666"));

我无法理解为什么它会在这一点上陷入困​​境,对此你有什么想法吗?

3 个答案:

答案 0 :(得分:2)

根据您的问题,您的应用程序是客户端还是服务器或两者都不清楚。对于编写蓝牙客户端 - 服务器应用程序,任何实例的Android手机都扮演服务器或客户端的单一角色。如果您的手机是服务器,则需要使用方法listenUsingRfcommWithServiceRecord()侦听来自其他蓝牙设备的连接。然后使用accept()完成连接。

如果Android手机充当客户端,它将启动与其他设备的蓝牙连接。对于这种情况,需要您的广播接收器。我们需要使用startDiscovery()方法扫描可用的蓝牙设备。当找到新的蓝牙设备时,将调用您的广播接收器的onReceive()。要连接到此找到的设备,请使用所需的UUID调用createRfcommSocketToServiceRecord()。

希望这有帮助。

答案 1 :(得分:0)

这可能很明显,但您是否实例化了BluetoothAdapter? Accept Thread使用适配器而不初始化它。

myBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

答案 2 :(得分:0)

在收听时,将发现名称设置为特定值,然后在广播接收器中使用listenUsingRfcommWithServiceRecord方法。

private class AcceptTask extends AsyncTask<UUID,Void,BluetoothSocket> {

    @Override
    protected BluetoothSocket doInBackground(UUID... params) {
        String name = mBtAdapter.getName();
        try {
            //While listening, set the discovery name to a specific value
            mBtAdapter.setName(SEARCH_NAME);
            BluetoothServerSocket socket = mBtAdapter.listenUsingRfcommWithServiceRecord("BluetoothRecipe", params[0]);
            BluetoothSocket connected = socket.accept();

            //Reset the BT adapter name
            mBtAdapter.setName(name);
            return connected;
        } catch (IOException e) {
            e.printStackTrace();
            mBtAdapter.setName(name);
            return null;
        }
    }

    @Override
    protected void onPostExecute(BluetoothSocket socket) {
        if(socket == null) {
            return;
        }
        mBtSocket = socket;
        ConnectedTask task = new ConnectedTask();
        task.execute(mBtSocket);
    }

}
// End