alert.setPositiveButton("扫描设备",
new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog,
int which) {
// TODO Auto-generated method stub
// Create a BroadcastReceiver for ACTION_FOUND
try {

Intent intent = new Intent(
BluetoothAdapter.ACTION_REQUEST_ENABLE);
// 设置蓝牙可见性,最多300秒
intent.putExtra(
BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION,
300);
startActivity(intent);//请求是否打开蓝牙
final Field field = dialog.getClass()
.getSuperclass()
.getDeclaredField("mShowing");
field.setAccessible(true);
adapter.startDiscovery();//开始扫描
alert.setTitle("正在扫描...").show();
mReceiver = new BroadcastReceiver() {
public void onReceive(Context context,
Intent intent) {
// Register the BroadcastReceiver
IntentFilter filter = new IntentFilter(
BluetoothDevice.ACTION_FOUND);
registerReceiver(mReceiver, filter);//注册 isCane = adapter.isDiscovering(); String action = intent.getAction(); if (BluetoothDevice.ACTION_FOUND
.equals(action)) { BluetoothDevice device = intent
.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); if (device.getBondState() != BluetoothDevice.BOND_BONDED) { HashMap<String, Object> map = new HashMap<String, Object>();
map.put(device.getName(),
device.getAddress());
listItem.add(map);
listItemAdapter
.notifyDataSetChanged();
}
} else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED
.equals(action)) {
alert.setTitle("搜索完成").show();
adapter.cancelDiscovery();
}
}
};
if (isCane) {
field.set(dialog, false);
} else {
field.set(dialog, true);
} } catch (Exception e) {
e.printStackTrace();
}
}
});
AlertDialog ad = alert.create();
ad.show();
}
});大神看看有什么问题!