问题描述
我开发一个蓝牙聊天应用程序。问题是,当我启用蓝牙应用程序启用蓝牙,但会导致强制关闭。下一次我启动相同的应用程序(带蓝牙功能),它工作顺利!我已经搜查,只得到了一些信息说,当我开始的意图启用蓝牙的code收益不等待意向的结果
公共无效的run(){
// 1.检查蓝牙已启用
如果(!blue.isEnabled()){
意图enable_Bluetooth =新的意图(
BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enable_Bluetooth,1);
}
// 2.启动蓝牙服务器
尝试 {
服务器= blue.listenUsingRfcommWithServiceRecord(DHIRAJ
MY_UUID);
第一:
声明在应用程序清单文件中的蓝牙许可(S)。例如:
<舱单...>
<使用-权限的Android:名称=android.permission.BLUETOOTH/>
...
< /舱单>
设置蓝牙:
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
如果(mBluetoothAdapter == NULL){
//设备不支持蓝牙
}
启用蓝牙:
如果(!mBluetoothAdapter.isEnabled()){
意图enableBtIntent =新的意图(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent,REQUEST_ENABLE_BT);
}
查找设备:
设置< BluetoothDevice类> pairedDevices = mBluetoothAdapter.getBondedDevices();
//如果有配对设备
如果(pairedDevices.size()大于0){
//循环配对设备
对于(BluetoothDevice类设备:pairedDevices){
//添加姓名和地址到一个数组适配器在ListView显示
mArrayAdapter.add(device.getName()+\ N+ device.getAddress());
}
}
发现设备:
//创建一个BroadcastReceiver的ACTION_FOUND
私人最终的BroadcastReceiver mReceiver =新的BroadcastReceiver(){
公共无效的onReceive(上下文的背景下,意图意图){
串动= intent.getAction();
//当发现找到的设备
如果(BluetoothDevice.ACTION_FOUND.equals(动作)){
//从意图获取BluetoothDevice类对象
BluetoothDevice类设备=
intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
//添加姓名和地址到一个数组适配器在ListView显示
mArrayAdapter.add(device.getName()+\ N+ device.getAddress());
}
}
};
//注册的BroadcastReceiver
IntentFilter的过滤器=新的IntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver(mReceiver,过滤器); //不要忘记的onDestroy期间注销
启用发现
意图discoverableIntent =新
意图(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION,300);
startActivity(discoverableIntent)
I am developing a Bluetooth chat application. The problem is that when i enable Bluetooth the application enables Bluetooth but causes force close. the next time i launch the same application(with Bluetooth enabled) it works smoothly ! i have searched and only got some information saying that when i start the intent for enable Bluetooth the code proceeds not waiting for the result of Intent
public void run() {
// 1. Check if Bluetooth is Enabled
if (!blue.isEnabled()) {
Intent enable_Bluetooth = new Intent(
BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enable_Bluetooth, 1);
}
// 2. Start Bluetooth Server
try {
Server = blue.listenUsingRfcommWithServiceRecord("dhiraj",
MY_UUID);
first:
Declare the Bluetooth permission(s) in your application manifest file. For example:
<manifest ... >
<uses-permission android:name="android.permission.BLUETOOTH" />
...
</manifest>
Setting up bluetooth:
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (mBluetoothAdapter == null) {
// Device does not support Bluetooth
}
Enable bluetooth:
if (!mBluetoothAdapter.isEnabled()) {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}
Finding devices:
Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
// If there are paired devices
if (pairedDevices.size() > 0) {
// Loop through paired devices
for (BluetoothDevice device : pairedDevices) {
// Add the name and address to an array adapter to show in a ListView
mArrayAdapter.add(device.getName() + "\n" + device.getAddress());
}
}
discovering devices:
// Create a BroadcastReceiver for ACTION_FOUND
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
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);
// Add the name and address to an array adapter to show in a ListView
mArrayAdapter.add(device.getName() + "\n" + device.getAddress());
}
}
};
// Register the BroadcastReceiver
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver(mReceiver, filter); // Don't forget to unregister during onDestroy
Enabling discovery
Intent discoverableIntent = new
Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);
startActivity(discoverableIntent)
这篇关于Android的蓝牙启用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!