Using the Android RecognizerIntent with a bluetooth headset

Manifest permission <uses-permission android:name=”android.permission.BLUETOOTH” /> <uses-permission android:name=”android.permission.BROADCAST_STICKY” /> <uses-permission android:name=”android.permission.MODIFY_AUDIO_SETTINGS” /> Create an inner class BluetoothHelper extends BluetoothHeadSetUtils in your Activity or Service. Declare a class member mBluetoothHelper and instantiate it in onCreate() BluetoothHelper mBluetoothHelper; @Override public void onCreate() { mBluetoothHelper = new BluetoothHelper(this); } @Override onResume() { mBluetoothHelper.start(); } @Override onPause() { mBluetoothHelper.stop(); } … Read more

Android sample bluetooth code to send a simple string via bluetooth

private OutputStream outputStream; private InputStream inStream; private void init() throws IOException { BluetoothAdapter blueAdapter = BluetoothAdapter.getDefaultAdapter(); if (blueAdapter != null) { if (blueAdapter.isEnabled()) { Set<BluetoothDevice> bondedDevices = blueAdapter.getBondedDevices(); if(bondedDevices.size() > 0) { Object[] devices = (Object []) bondedDevices.toArray(); BluetoothDevice device = (BluetoothDevice) devices[position]; ParcelUuid[] uuids = device.getUuids(); BluetoothSocket socket = device.createRfcommSocketToServiceRecord(uuids[0].getUuid()); socket.connect(); outputStream = socket.getOutputStream(); … Read more

IOException: read failed, socket might closed – Bluetooth on Android 4.3

I have finally found a workaround. The magic is hidden under the hood of the BluetoothDevice class (see https://github.com/android/platform_frameworks_base/blob/android-4.3_r2/core/java/android/bluetooth/BluetoothDevice.java#L1037). Now, when I receive that exception, I instantiate a fallback BluetoothSocket, similar to the source code below. As you can see, invoking the hidden method createRfcommSocket via reflections. I have no clue why this method is … Read more