【问题标题】:How to get user defined data with deviceName and device mac address through Bluetooth device scanning in android如何通过android中的蓝牙设备扫描获取带有deviceName和设备mac地址的用户定义数据
【发布时间】:2016-04-13 05:37:39
【问题描述】:

一个应用在两台设备上运行。通过这个应用程序,两个用户在不同的设备上注册,而这些数据存储在本地。现在,在任何一台设备上运行的应用程序会在第二台设备上的应用程序未运行时通过蓝牙扫描第二台设备。

现在我想通过蓝牙适配器类在第一台设备上的扫描时间内获取第二台设备上的应用数据。

我可以通过BlueToothAdapter 类的getName()getAddress() 方法获取设备名称和设备地址。

如何使用 BlueToothAdapter 附加数据以及如何从 其他设备上的蓝牙适配器?

【问题讨论】:

    标签: android bluetooth


    【解决方案1】:

    更新:

    在 OnCreate 中:

    mChatService = new BluetoothChatService(mContext, mHandler);
    for (BluetoothDevice device : MainActivity.mBluetoothAdapter.getBondedDevices()) {
        mChatService.connect(device, false);
    }
    

    在该文件的后面:

    protected void sendMessage(String s) throws IOException {
       //outputStream.write(s.getBytes());
       if(s.length() != 0) {
           messagesAdapter.add(s);//UI On local device
           messagesAdapter.notifyDataSetChanged();
           mChatService.write(temp.getBytes());                 
           messageTextbox.setText("");
           Toast.makeText(mContext, "Message sent!", Toast.LENGTH_LONG).show();
       }
    }
    
    private final Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            //FragmentActivity activity = getActivity();
            switch (msg.what) {
                case Constants.MESSAGE_STATE_CHANGE:
                    switch (msg.arg1) {
                        case BluetoothChatService.STATE_CONNECTED:
                            Toast.makeText(mContext, "Connected(1)", Toast.LENGTH_SHORT).show();
                            break;
                        case BluetoothChatService.STATE_CONNECTING:
                            //setStatus(R.string.title_connecting);
                            Toast.makeText(mContext, "Connecting(1)", Toast.LENGTH_SHORT).show();
                            break;
                        case BluetoothChatService.STATE_LISTEN:
                        case BluetoothChatService.STATE_NONE:
                            Toast.makeText(mContext, "Not Con(1)", Toast.LENGTH_SHORT).show();
                            break;
                    }
                    break;
                case Constants.MESSAGE_WRITE:
                    byte[] writeBuf = (byte[]) msg.obj;
                    // construct a string from the buffer
                    String writeMessage = new String(writeBuf);
                    break;
                case Constants.MESSAGE_READ:
                    byte[] readBuf = (byte[]) msg.obj;
                    // construct a string from the valid bytes in the buffer
                    String readMessage = new String(readBuf, 0, msg.arg1);
                    Toast.makeText(mContext, "NEW:" + readMessage, Toast.LENGTH_LONG).show();//------- This is where the message is received
                    if(!readMessage.equals("")){
                        messagesAdapter.add(readMessage);
                    }
                    //mConversationArrayAdapter.add(mConnectedDeviceName + ":  " + readMessage);
                    break;
                case Constants.MESSAGE_DEVICE_NAME:
                    // save the connected device's name
                    //mConnectedDeviceName = msg.getData().getString(Constants.DEVICE_NAME);
                    if (null != mContext) {
                        Toast.makeText(mContext, "Connected to "
                                + MainActivity.address, Toast.LENGTH_SHORT).show();
                    }
                    break;
                case Constants.MESSAGE_TOAST:
                    if (null != mContext) {
                        Toast.makeText(mContext, msg.getData().getString(Constants.TOAST),
                                Toast.LENGTH_SHORT).show();
                    }
                    break;
            }
        }
    };
    

    我不能说我已经在服务中尝试过,但您可以尝试将处理程序放入服务中。看看它是否仍然收到蓝牙消息。我知道这在活动中有效。


    这是一个 example 从 Google 进行蓝牙聊天。包含显示如何查找附近设备、配对和在设备之间发送消息的代码。

    在这个例子中,有一个DeviceListActivity 可以找到附近的设备:

    private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
    
            // When discovery finds a device
            if (BluetoothDevice.ACTION_FOUND.equals(action)) {
                // Get the BluetoothDevice object from the Intent
                BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                // If it's already paired, skip it, because it's been listed already
                if (device.getBondState() != BluetoothDevice.BOND_BONDED) {
                    mNewDevicesArrayAdapter.add(device.getName() + "\n" + device.getAddress());
                }
                // When discovery is finished, change the Activity title
            } else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
                setProgressBarIndeterminateVisibility(false);
                setTitle(R.string.select_device);
                if (mNewDevicesArrayAdapter.getCount() == 0) {
                    String noDevices = getResources().getText(R.string.none_found).toString();
                    mNewDevicesArrayAdapter.add(noDevices);
                }
            }
        }
    };
    

    如您所见,这条线正在获取附近的设备名称和地址:

    mNewDevicesArrayAdapter.add(device.getName() + "\n" + device.getAddress());
    

    如果您想在应用未运行时运行代码,请查看 unbound servicesanother example。所以在 onDestroy 中你可以启动服务,在 onCreate 中停止服务。

    【讨论】:

    • 感谢您,但希望在我已经收到设备名称和设备地址的情况下使用 BluetoothAdaper 在应用程序中获取用户的其他一些数据
    猜你喜欢
    • 2015-12-26
    • 2022-06-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-19
    • 2015-02-02
    • 1970-01-01
    • 2020-10-27
    相关资源
    最近更新 更多