简体   繁体   中英

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 . My task is to print a bill by using Bluetooth printer from my android app .

For the Bluetooth Searching Activity you require following things,

Add Permissions in to your 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.

Activity class , onCreate() method

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

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

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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