本文介绍了如何检测蓝牙设备,并得到检测设备从Android应用程序的蓝牙地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我要的检测蓝牙设备,并从我的Android应用程序获取检测设备的蓝牙地址。我的任务是通过使用蓝牙打印机从我的Android应用程序,打印账单。
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,
在添加权限到您的AndroidManifest.xml
Add Permissions in to your AndroidManifest.xml
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.BLUETOOTH" />
最低API级别7和Android 2.1的版本是必需的。
Minimum API level 7 and Android 2.1 version is required.
活动类,的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 );
}
创建的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() );
}
}
};
这篇关于如何检测蓝牙设备,并得到检测设备从Android应用程序的蓝牙地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!