DavidBalas
DavidBalas

Reputation: 333

Send an int to a paired Bluetooth device

I managed to create a ListView, search for devices and show them, on click connect to the selected device, but now I want to know how to send from one phone to the other one just an int or a boolean or something like this.
It's a little game, there is a winner and a loser - so I want to compare two variables and check who won, then display it.

My code so far:

Searching

bluetooth.startDiscovery();
  textview.setText("Searching, Make sure other device is available");
  IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
  registerReceiver(mReceiver, filter);

Displaying in a ListView

private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if (BluetoothDevice.ACTION_FOUND.equals(action)) {
            device = intent
                    .getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            mDeviceList.add(device.getName() + "\n" + device.getAddress());
            Log.i("BT", device.getName() + "\n" + device.getAddress());
            listView.setAdapter(new ArrayAdapter<String>(context,
                    android.R.layout.simple_list_item_1, mDeviceList));
        }
    }
};
      @Override
protected void onDestroy() {
    unregisterReceiver(mReceiver);
    super.onDestroy();
}

Pairing the devices

    private void pairDevice(BluetoothDevice device) {
    try {
        Log.d("pairDevice()", "Start Pairing...");
        Method m = device.getClass().getMethod("createBond", (Class[]) null);
        m.invoke(device, (Object[]) null);
        Log.d("pairDevice()", "Pairing finished.");
    } catch (Exception e) {
        Log.e("pairDevice()", e.getMessage());
    }
}

It works pretty well, both devices are paired.
Do I need a client and a server to transfer ints?

Upvotes: 0

Views: 1238

Answers (1)

MeetTitan
MeetTitan

Reputation: 3568

Here is some sample code from the android SDK: BluetoothChat

The key is to use a Service on both sides of the connection that both listens for incoming messages and sends outgoing messages. Then all your Activity has to worry about is interacting with the user, and telling the service to send the appropriate messages.

//METHOD FROM ACTIVITY
private void sendMessage(String message) {
    // Check that we're actually connected before trying anything
    if (mChatService.getState() != BluetoothChatService.STATE_CONNECTED) {
        Toast.makeText(this, R.string.not_connected, Toast.LENGTH_SHORT).show();
        return;
    }
    // Check that there's actually something to send
    if (message.length() > 0) {
        // Get the message bytes and tell the BluetoothChatService to write
        byte[] send = message.getBytes();
        mChatService.write(send);
        // Reset out string buffer to zero and clear the edit text field
        mOutStringBuffer.setLength(0);
        mOutEditText.setText(mOutStringBuffer);
    }
}

//METHOD FROM SERVICE
public void write(byte[] out) {
    // Create temporary object
    ConnectedThread r;
    // Synchronize a copy of the ConnectedThread
    synchronized (this) {
        if (mState != STATE_CONNECTED) return;
        r = mConnectedThread;
    }
    // Perform the write unsynchronized
    r.write(out);
}

Upvotes: 1

Related Questions