如何通过android中的蓝牙设备扫描获取带有deviceName和设备mac地址的用户定义数据

时间:2016-01-08 13:53:53

标签: android bluetooth

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

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

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

如何使用BlueToothAdapter附加数据以及如何从中获取此数据 BlueToothAdapter在其他设备上?

1 个答案:

答案 0 :(得分:0)

<强>更新

在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中停止服务。