【问题标题】:Send an int to a paired Bluetooth device向配对的蓝牙设备发送 int
【发布时间】:2015-08-04 22:50:14
【问题描述】:

我设法创建了一个 ListView,搜索设备并显示它们,点击连接到选定的设备,但现在我想知道如何从一部手机向另一部手机发送一个整数或布尔值或类似的东西这个。
这是一个小游戏,有赢家和输家 - 所以我想比较两个变量并检查谁赢了,然后显示出来。

到目前为止我的代码:

搜索

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

在列表视图中显示

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();
}

配对设备

    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());
    }
}

效果很好,两台设备都已配对。
我需要客户端和服务器来传输整数吗?

【问题讨论】:

    标签: android bluetooth


    【解决方案1】:

    以下是来自 android SDK 的一些示例代码:BluetoothChat

    关键是在连接的两端都使用Service,它既监听传入消息又发送传出消息。那么你所有的Activity 需要担心的是与用户交互,并告诉服务发送适当的消息。

    //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);
    }
    

    【讨论】:

    • 所以我可以使用 sendMessage(int XXX) 吗?
    • @David,不,请查看链接并完整查看示例的来源。我还编辑了我的答案,您可能需要重新阅读。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多