简体   繁体   English

如何从Android应用程序检测蓝牙设备并获取检测到的设备的蓝牙地址

[英]How to detect bluetooth device and get the bluetooth address of detected device from android app

I want to the detect bluetooth device and get the bluetooth address of detected device from my android app . 我想检测蓝牙设备并从我的android应用程序获取检测到的设备的蓝牙地址。 My task is to print a bill by using Bluetooth printer from my android app . 我的任务是使用android应用程序中的Bluetooth打印机打印账单。

For the Bluetooth Searching Activity you require following things, 对于蓝牙搜索活动,您需要执行以下操作,

Add Permissions in to your AndroidManifest.xml 在您的AndroidManifest.xml中添加权限

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

Minimum API level 7 and Android 2.1 version is required. 需要最低API级别7和Android 2.1版本。

Activity class , onCreate() method 活动类, onCreate()方法

private static BluetoothAdapter mBtAdapter;

// Register for broadcasts when a device is discovered
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
this.registerReceiver(mReceiver, filter);

// Register for broadcasts when discovery has finished
filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
this.registerReceiver(mReceiver, filter);


filter = new IntentFilter( BluetoothAdapter.ACTION_DISCOVERY_STARTED );
this.registerReceiver( mReceiver, filter );

// Get the local Bluetooth adapter
mBtAdapter = BluetoothAdapter.getDefaultAdapter();


if ( !mBtAdapter.isEnabled()) 
{
    Intent enableBtIntent = new Intent( BluetoothAdapter.ACTION_REQUEST_ENABLE );
    startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT );
}           

Create BroadCastReceiver in same Activity 在同一Activity创建BroadCastReceiver

private final BroadcastReceiver mReceiver = new BroadcastReceiver() 
{
    @Override
    public void onReceive(Context context, Intent intent) 
    {
        try
        {
            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);

                String deviceName = device.getName();
                String deviceAddress = device.getAddress();

                            System.out.println ( "Address : " + deviceAddress );
            } 
        }
        catch ( Exception e )
        {
            System.out.println ( "Broadcast Error : " + e.toString() );
        }
    }
};

Once you find the device I would expect you would need to connect to it. 找到设备后,我希望您需要连接到该设备。

Below is an example similar to the one posted above but it also prints out the services on each device. 下面是一个与上面发布的示例类似的示例,但是它还打印了每台设备上的服务。

http://digitalhacksblog.blogspot.com/2012/05/android-example-bluetooth-discover-and.html http://digitalhacksblog.blogspot.com/2012/05/android-example-bluetooth-discover-and.html

I don't have any examples of connecting to a printer but I do have an example for connecting to devices and sending data which may be helpful. 我没有连接打印机的任何示例,但有连接设备和发送数据的示例,这可能会有所帮助。 The first one is connecting to a Windows PC running an SPP server and the second is connecting to an Arduino. 第一个连接到运行SPP服务器的Windows PC,第二个连接到Arduino。

http://digitalhacksblog.blogspot.com/2012/05/android-example-bluetooth-simple-spp.html http://digitalhacksblog.blogspot.com/2012/05/arduino-to-android-turning-led-on-and.html http://digitalhacksblog.blogspot.com/2012/05/android-example-bluetooth-simple-spp.html http://digitalhacksblog.blogspot.com/2012/05/arduino-to-android-turning-led-on -and.html

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM