1

我在一个使用蓝牙套接字的 Android 项目中。该应用程序是客户端套接字。但是应用程序在工作时崩溃。谁能帮我解决这个问题。

这是代码

public class READ extends AppCompatActivity {
    private static String btAdress = "00:10:60:D1:95:CD";
    BluetoothSocket mmSocket;
    BluetoothDevice mmDevice;
    BluetoothAdapter btAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_read);
    }
    public void ConnectThread(View v){
        BluetoothDevice device;
        device = btAdapter.getRemoteDevice(btAdress);
        ConnectT(device);
    }
    public void ConnectT(BluetoothDevice device) {
        // Use a temporary object that is later assigned to mmSocket,
        // because mmSocket is final
        mmDevice = device;
        BluetoothSocket tmp = null;
        String uuid = "a60f35f0-b93a-11de-8a39-08002009c666";
        // Get a BluetoothSocket to connect with the given BluetoothDevice
        try {
            // MY_UUID is the app's UUID string, also used by the server code
            tmp = device.createRfcommSocketToServiceRecord(UUID.fromString(uuid));
        } catch (IOException e) { }
        mmSocket = tmp;
        try {
            // Connect the device through the socket. This will block
            // until it succeeds or throws an exception
            mmSocket.connect();
        } catch (IOException nullException) {
            // Unable to connect; close the socket and get out
            try {
                mmSocket.close();
            } catch (IOException closeException) { }
        }
    }
}

提前致谢。

4

0 回答 0