Android:只要可用,就通过蓝牙处理数据

时间:2016-02-01 23:19:43

标签: java android bluetooth

我想知道是否可以检测到BluetoothSocket的InputStream上何时有数据可用。如果有可用数据,应用程序会自行处理。我尝试过多次但没有成功。我是android开发的新手,所以我可能完全错了。

有人有一个例子,或者这是不可能的

修改

这是我的一些代码。 我扩展我的应用程序以在活动之间切换时保持我的蓝牙连接活着

public class Bluetooth extends Application {
    public static final int HANDLE_READ = 1;
    BluetoothDevice device;
    BluetoothSocket socket;
    private InputStream inputStream;
    private OutputStream outputStream;
    private static final UUID Uuid = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
    BluetoothConnect bluetoothConnect;
    public BluetoothReceiveData bluetoothReceiveData;
    public static String data;

    public void write(String msg) {
        try {
            byte[] b = msg.getBytes(StandardCharsets.UTF_8);
            outputStream.write(b);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public boolean setBtConnection(BluetoothDevice device) {
        try {
            this.device = device;
            bluetoothConnect = new BluetoothConnect(device, Uuid);
            bluetoothConnect.run();
            socket = bluetoothConnect.mmSocket;
            outputStream = socket.getOutputStream();
            inputStream = socket.getInputStream();
            bluetoothReceiveData = new BluetoothReceiveData(inputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return socket.isConnected();
    }

    public void disconnect() {
        try {
            bluetoothConnect.cancel();
            socket.close();
            device = null;
            inputStream = null;
            outputStream = null;
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    class Receiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            Integer state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, -1);

            if (BluetoothAdapter.ACTION_STATE_CHANGED.equals(action)) {
                if (state == BluetoothAdapter.STATE_OFF) {

                    //Restart app when bluetooth is disconnected
                    Intent mStartActivity = new Intent(context, ConnectToDevice.class);
                    mStartActivity.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    int mPendingIntentId = 123456;
                    PendingIntent mPendingIntent = PendingIntent.getActivity(context, mPendingIntentId, mStartActivity, PendingIntent.FLAG_CANCEL_CURRENT);
                    AlarmManager mgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
                    mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 100, mPendingIntent);
                    System.exit(0);

                    Toast.makeText(context, "You have turned your bluetooth off", Toast.LENGTH_SHORT).show();
                } else if (state == BluetoothAdapter.STATE_ON) {
                    Toast.makeText(context, "You have turned your bluetooth on", Toast.LENGTH_SHORT).show();
                } else if (state == BluetoothAdapter.STATE_CONNECTED) {
                    Toast.makeText(context, "You are connected now", Toast.LENGTH_SHORT).show();
                } else if (state == BluetoothAdapter.STATE_DISCONNECTED) {
                    Toast.makeText(context, "You are disconnected now", Toast.LENGTH_SHORT).show();
                }
            }
        }
    }
}

然后我试图制作一个线程来处理收到的数据

public class BluetoothReceiveData extends Thread {
    private final InputStream mmInStream;

    //creation of the connect thread
    public BluetoothReceiveData(InputStream inputStream) {
        mmInStream = inputStream;
    }

    public void run() {
        while(true){
            try {
                byte[] buffer = new byte[mmInStream.available()];
                int bytes;
                if (buffer.length > 0) {
                    // Keep looping to listen for received messages
                    bytes = mmInStream.read(buffer);            //read bytes from input buffer
                    // Send the obtained bytes to the UI Activity via handler
                    Bluetooth.data = new String(buffer, 0, bytes);
                }
            } catch (IOException e) {
                e.printStackTrace();
                break;
            }
        }
    }
}

现在我将数据保存为公共字符串中的String 这似乎有效,但程序将永远不会出现,除非我将其更改为

while(mmInStream.available() > 0)

但是当我收到数据时,它不会在线程中处理

我在需要数据的活动中调用bluetoothReveiveData.run()。 但随后代码一直在旋转

0 个答案:

没有答案