【问题标题】:Disconnecting a bluetooth device断开蓝牙设备
【发布时间】:2016-03-14 00:21:30
【问题描述】:

我有一个连接到设备(在本例中为蓝牙信用卡机器)的功能,如下所示:

private void pinPar(final String name, final String address) {
    MainActivity.this.runOnUiThread(new Runnable() {
        @Override
        public void run() {
            pinpadSelected = new PinpadObject(name, address, false);
            BluetoothConnectionProvider bluetoothConnectionProvider = new BluetoothConnectionProvider(MainActivity.this, pinpadSelected);
            bluetoothConnectionProvider.setDialogMessage("Connecting to pinpad");
            bluetoothConnectionProvider.setWorkInBackground(false);
            bluetoothConnectionProvider.setConnectionCallback(new StoneCallbackInterface() {
                                                                  @Override
                                                                  public void onSuccess() {

                                                                      Toast.makeText(getApplicationContext(), "Pinpad connected", Toast.LENGTH_SHORT).show();
                                                                      out.println("Connected to " + name + " at " + address);

                                                                  }

                                                                  @Override
                                                                  public void onError() {
                                                                      Toast.makeText(getApplicationContext(), "Connection failed", Toast.LENGTH_SHORT).show();
                                                                      out.println("Failed connecting to "+ name + " at " + address);
                                                                  }
                                                              }

            );
            bluetoothConnectionProvider.execute();
        }
    });
}

我希望创建一个类似的函数 pinUnpar 来简单地关闭该连接,但 bluetoothConnectionProvider 没有方法 close() 或类似的东西。我怎样才能做到这一点?

【问题讨论】:

  • 伙伴,BluetoothConnectionProvider 是否在您导入的库中?因为在我的一生中,我在 android API 文档中找不到这个。
  • @gedo 这解释了我在谷歌上搜索时的痛苦。我以为它是 android API 的一部分,但实际上它是 this SDK 的一部分
  • 提供的蓝牙API比你使用imo的那个SDK好很多(我看了看),使用它创建连接和控制它要容易得多。
  • @gedo 如果您想使用普通的蓝牙 API 提供和回答,我很乐意接受:)

标签: android bluetooth


【解决方案1】:

好的,我已经有一段时间没有处理安卓上的蓝牙了,但是这里有。 使用蓝牙连接设备的方式有很多种,但我特别喜欢一种简单的方式,因为它不需要扫描您要连接的设备,也不需要配对。如下:

首先你需要一个你的客户端和服务器都知道的通用 UUID,因为在这种情况下你的服务器是蓝牙信用卡机器,你需要找出它用于连接的 UUID 是什么(不应该太难了,如果机器说明书上没有写,那你可以用笔记本自己检测)。

客户端代码:-

 BluetoothAdapter adapter;
 adapter= BluetoothAdapter.getDefaultAdapter();
 BluetoothDevice device;
 device= adapter.getRemoteDevice(serverAddress); //address here would be the address value 
 //passed to your function
 BluetoothSocket socket= device.createInsecureRfcommSocketToServiceRecord(uuid);
 //here uuid is the UUID the device uses as mentioned perviously
 socket.connect();
 OutputStream ouput=socket.getOutputStream();
 InputStream input=socket.getInputStream();

就像这样,您与您的机器建立了连接,您可以在上面写任何东西和读任何东西。我假设你不是在编写信用卡机器,所以我省略了相应的服务器代码。

由于此代码使用简单的流和套接字,因此很容易关闭,就像打开一样容易。

编辑:-

这仅将 android API 用于 BT 连接,请注意此代码使用不安全的 rfcomm,这意味着它容易受到 MITMA 和其他此类攻击。如果您不希望这样做,您可以替换

device.createInsecureRfcommSocketToServiceRecord(uuid)

与

device.createRfcommSocketToServiceRecord(uuid);

【讨论】:

    猜你喜欢
    • 2023-03-23
    • 1970-01-01
    • 2016-01-30
    • 2016-06-29
    • 1970-01-01
    • 1970-01-01
    • 2015-12-18
    • 2018-01-20
    • 1970-01-01
    相关资源
    最近更新 更多