samedi 25 avril 2015

Nothing Happen when i send data to bluetooth device from android


I am making an android app when sending data to bluetooth nothing happen. I made two threads one for bluetooth connection one for sending data every thing works fine but not recieving the data on the target mobile plz help. Thnkx in adv

private class ConnectThread extends Thread {
             private final BluetoothSocket mmSocket;
             private BluetoothAdapter mybluetoothAdapter;
             private final BluetoothDevice mmDevice;
             private final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805f9b34fb");
             public ConnectThread(BluetoothDevice device) {
                 BluetoothSocket tmp = null;
                 mmDevice = device;
                 try {
                     tmp = device.createRfcommSocketToServiceRecord(MY_UUID);
                 } catch (IOException e) { }
                 mmSocket = tmp;
             }


             public void run() {
                 //mybluetoothAdapter.cancelDiscovery();
                 try {
                     mmSocket.connect();
                 } catch (IOException connectException) {
                     try {
                         mmSocket.close();
                     } catch (IOException closeException) { }
                     return;
                 }
                 ConnectedThread connected=new ConnectedThread(mmSocket);
                 connected.start();
             }
             public void cancel() {
                 try {
                     mmSocket.close();
                 } catch (IOException e) { }
             }
         }

    private class ConnectedThread extends Thread {
        private final BluetoothSocket mmSocket;
        private final InputStream mmInStream;
        private final OutputStream mmOutStream;
        public ConnectedThread(BluetoothSocket socket) {
            mmSocket = socket;
            InputStream tmpIn = null;
            OutputStream tmpOut = null;
            try {
                tmpIn = socket.getInputStream();
                tmpOut = socket.getOutputStream();
            } catch (IOException e) { }
            mmInStream = tmpIn;
            mmOutStream = tmpOut;
        }
        public void run() {
            byte[] buffer = new byte[1024];
            int begin = 0;
            int bytes = 0;
            while (true) {
                try {
                    bytes += mmInStream.read(buffer, bytes, buffer.length - bytes);
                    for(int i = begin; i < bytes; i++) {
                        if(buffer[i] == "#".getBytes()[0]) {
                            mHandler.obtainMessage(1, begin, i, buffer).sendToTarget();
                            begin = i + 1;
                            if(i == bytes - 1) {
                                bytes = 0;
                                begin = 0;
                            }
                        }
                    }
                } catch (IOException e) {
                    break;
                }
            }
        }
        public void write(byte[] bytes) {
            try {
                mmOutStream.write(bytes);
            } catch (IOException e) { }
        }
        public void cancel() {
            try {
                mmSocket.close();
            } catch (IOException e) { }
        }
    }


Aucun commentaire:

Enregistrer un commentaire