I am trying to get all data from a bluetooth device (Google Daydream Controller) to react to certain user inputs within an android application. I found a web application (https://github.com/mrdoob/daydream-controller.js) that pretty much reads the data as I would need it. Really I only need the button feedback for the app button.
But I am not able to connect! There is always an error when trying to connect. Other people have experience similar but in different setups.. I have not been able to find a solution
public class TryAgain extends AppCompatActivity {
private static final String UUID_SERIAL_PORT_PROFILE = "00001101-0000-1000-8000-00805F9B34FB";
private static final String TAG = "tag";
BluetoothAdapter mBluetoothAdapter;
BluetoothDevice mDevice;
private BluetoothSocket mSocket = null;
private BufferedReader mBufferedReader = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_try_again);
findBT();
try {
openDeviceConnection(mDevice);
} catch (IOException e) {
e.printStackTrace();
}
}
void findBT()
{
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if(mBluetoothAdapter == null)
{
System.out.println("No bluetooth adapter available");
}
if(!mBluetoothAdapter.isEnabled())
{
Intent enableBluetooth = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBluetooth, 0);
}
Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
if(pairedDevices.size() > 0)
{
for(BluetoothDevice device : pairedDevices)
{
if(device.getName().equals("Daydream controller"))
{
mDevice = device;
break;
}
}
}
System.out.println("Bluetooth Device Found");
}
private void openDeviceConnection(BluetoothDevice device)
throws IOException {
InputStream aStream = null;
InputStreamReader aReader = null;
try {
mSocket = device.createRfcommSocketToServiceRecord( getSerialPortUUID() );
mSocket.connect();
aStream = mSocket.getInputStream();
aReader = new InputStreamReader( aStream );
mBufferedReader = new BufferedReader( aReader );
} catch ( IOException e ) {
Log.e( TAG, "Could not connect to device", e );
close( mBufferedReader );
close( aReader );
close( aStream );
close( mSocket );
throw e;
}
}
private void close(Closeable aConnectedObject) {
if ( aConnectedObject == null ) return;
try {
aConnectedObject.close();
} catch ( IOException e ) {
}
aConnectedObject = null;
}
private UUID getSerialPortUUID() {
return UUID.fromString( UUID_SERIAL_PORT_PROFILE );
}
}
The first step would be to read the data stream that the device sends and then extract the information to get the clicker information. Of course connecting to the device is essential and that's where I am struggling right now.
Error message:
java.io.IOException: read failed, socket might closed or timeout, read ret: -1
You need to call
openDeviceConnection
from a background thread like this:Put this code below
findBT()