连接到特定的蓝牙设备与点击蓝牙、连接到、设备

2023-09-06 06:51:59 作者:短

我想用我的Andr​​oid应用程序连接到特定的设备,到现在为止我所能够做的就是配对项目这样做:

I'm trying to connect to a specific device using my Android APP, until now what I was able to do is get the paired items doing this :

BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
Set < BluetoothDevice > pairedDevices = bluetoothAdapter.getBondedDevices();
if (pairedDevices.size() > 0) {
    for (BluetoothDevice device: pairedDevices) {
        mDeviceName.add(device.getName());
        mDeviceMAC.add(device.getAddress());

    }
}
bluetoothClass.setDeviceName(mDeviceName);
bluetoothClass.setDeviceMac(mDeviceMAC);

我在哪里获得 MAC 设备名称我的所有配对设备的。我想要做的事情是,当我选择一个它连接到蓝牙设备。

Where I get the MAC and the Device name of all of my paired devices. The thing that I would like to do is when I select one it connects to the Bluetooth device.

在一个三星S4 当我打开蓝牙它弹窗一个对话框加入WTO要求包含所有我的配对设备,当我点击任何它连接(我我能够......)所以基本上我想这样做,因为现在我已经得到了配对设备(我不知道它是否得到了最好的方式,但它所做的),然后当任何人点击用户在连接到设备上。

On a Samsung S4 when I turn on the Bluetooth it popups a Dialog whitch contains all of my paired devices and when I click on anyone it connects (I've i'm able to ...) so basically I want to do this, since now I've been getting the paired devices (I don't know if it's the best way to get that but it does) and then when user click on anyone it connects to the device.

这件事情是这样的question但它不幸的是无人接听。

It's something like this question but it's unfortunately unanswered.

推荐答案

这是不可能给你这种格式中的一个例子,所以我只要你具有良好的样品和有用的链接,以帮助您了解的样本。

It's impossible to give you an example within this format, so I have provided you with a good sample and helpful links to help you understand the sample.

我建议你按照我所提供的步骤,然后,当你有具体的问题,你可以在这里把它与您遇到的code片段难同。

I recommend you follow the steps I have provided and then, when you have specific problems, you can bring it here, with the code snippet you are having difficulty with.

我建议你使用下载此示例code:结果 http://developer.android.com/samples/BluetoothChat/index.html

I recommend you use download this sample code: http://developer.android.com/samples/BluetoothChat/index.html

如果你还没有,这是很好的研究这个:结果 http://developer.android.com/reference/android/bluetooth/BluetoothDevice.html

If you haven't already, it's good to study this: http://developer.android.com/reference/android/bluetooth/BluetoothDevice.html

这是一个很好的教程,他们有很多教程:结果 http://www.tutorialspoint.com/android/android_bluetooth.htm

This is a good tutorial and they have many tutorials: http://www.tutorialspoint.com/android/android_bluetooth.htm

您将需要下列权限在清单:

You will need the following permissions in your manifest:

<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>

这是一款意图是建议使用,进行检查,看是否BT已启用:

This is one intent that is advisable to use, to check to see if BT is enabled:

if (!mBluetoothAdapter.isEnabled()) {
    android.content.Intent enableIntent = new android.content.Intent(
           android.bluetooth.BluetoothAdapter.ACTION_REQUEST_ENABLE);
    startActivityForResult(enableIntent, REQUEST_ENABLE_BT);
}

和使您的设备可被其他设备:

and to make your device discoverable to other devices:

if (mBluetoothAdapter.getScanMode() !=
        android.bluetooth.BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE) {
    android.content.Intent discoverableIntent =
        new android.content.Intent(
            android.bluetooth.BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
    discoverableIntent.putExtra(
        android.bluetooth.BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION,
                300); // You are able to set how long it is discoverable.
    startActivity(discoverableIntent);
}

我在我的答案这里:

您能避免使用意向来搜索配对设备。什么时候  连接到未配对的装置,通知将弹出  要求配对的设备。一旦配对此消息不应该显示  再次对这些设备的连接应该是自动的(根据  你如何编写程序)。

You can avoid using an intent to search for paired devices. When connecting to a device that is not paired, a notification will pop up asking to pair the devices. Once paired this message should not show again for these devices, the connection should be automatic (according to how you have written your program).

我使用的意图,启用蓝牙,并且使我的设备  发现的,然后我建立了我的code连接,和preSS一个按钮,  连接。在你的情况,你需要确保你的配件  发现也。在我来说,我使用的唯一的UUID,并且两个设备  必须认识到这一点进行连接。如果你是这只能使用  编程两个设备,无论是否安卓或者一个Android和  一个其他的设备类型。

I use an intent to enable bluetooth, and to make my device discoverable, I then set up my code to connect, and press a button to connect. In your case, you will need to ensure your accessories are discoverable also. In my case I use a unique UUID, and both devices must recognise this to connect. This can only be used if you are programming both devices, whether both are android or one android and one other device type.

您需要了解如何使用套接字,这是设备的通信方式。结果 我建议研究这两个链接:

You will need to understand how to use sockets, this is how the devices communicate. I recommend studying these two links:

http://developer.android.com/reference/android/bluetooth/ BluetoothSocket.html 结果的http://developer.android.com/reference/android/bluetooth/BluetoothServerSocket.html

套接字用于建立设备之间的连接。将会有一个服务器套接字和设备的插座(由许多因素,程序员,实际设备确定)。服务器套接字侦听传入连接,当一个连接被接受的设备连接,每一个简单的插座。

Sockets are used to set up connections between devices. There will be a server socket and device sockets (determined by many factors, the programmer, the actual devices). The server socket listens for incoming connections, when a connection is accepted the devices connect, each with a simple socket.

我不知道你了解线程多少。连接需要使用线程进行管理:

I am not sure how much you know about threading. The connection needs to be managed with threads:

http://developer.android.com/guide/components/流程和-threads.html 结果 http://android-developers.blogspot.com.au/2009 /05/painless-threading.html

在设备之间的连接是在线程管理与用户分离 界面线程。这是prevent手机从锁起来,把 建立,寻找和制作BT连接。

The connection between the devices is managed on threads separate from the User Interface thread. This is to prevent the phone from locking up while it is setting up, seeking and making a BT connection.

例如:

AcceptThread - 侦听的连接,并接受(通过ServerSocket的)的连接线。该线程可以较长时间等待一台设备与连接运行。

AcceptThread - the thread that listens for a connection and accepts the connection (via the serversocket). This thread can run for an extended time waiting for a device to connect with.

ConnectThread - 使连接到服务器的设备用于连接到将ServerSocket线程

ConnectThread - the thread that the device connecting to the server uses to connect to the serversocket.

ConnectedThread - 这是同时管理插槽之间的连接的螺纹

ConnectedThread - this is the thread that manages the connection between both sockets.

让我知道如果这可以帮助你。

Let me know if this helps you.