Help Bluetooth app?

I'm still new to Android development, but I'm currently working on a project that requires the Bluetooth interface to stay in constant communication with the Bluetooth device. I read on Service, IntentServiceand AsynTask, but I'm still confused. I think I should not use AsyncTask, because it is designed to handle a very short task? If I use IntentService, how can I create multiple streams to check if a Bluetooth device is connected, sending and receiving?

I am using BluetoothViewer as my link. It connectThreadhas a stream that, when connected to a Bluetooth device, then launches connectedThread.

thank

Ken

+1
source share
1 answer

I am using the ACTION_REQUEST_ENABLE intent startActivityForResult (). Then it calls:

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == imu.requestCode) {
            if (resultCode == RESULT_OK) {
            getPairedDevice();  // my function 
            initializeConnection(); // my function
        }
    }
}

initializeConnection () creates a new stream that initializes my input socket, creates another stream to handle Bluetooth input, and creates my output stream. Fragments of these features include:

myServerSocket = dev.createInsecureRfcommSocketToServiceRecord(uuid); 
myBluetooth.cancelDiscovery();
myServerSocket.connect();
myBluetoothInputThread = new BluetoothInputThread(myServerSocket, handler);                    myBluetoothInputThread.setPriority(Thread.MAX_PRIORITY); 
myBluetoothInputThread.start();
myBluetoothSocketOutputStream = myServerSocket.getOutputStream();

BluetoothInputThread extends the stream to create a separate input control process. This class communicates with its parent class through Handler.sendMessage

0
source

All Articles