【问题】
折腾:
期间,代码:public class MainActivity extends FragmentActivity implements
ActionBar.TabListener {
.......
......
@Override
protected void onCreate(Bundle savedInstanceState) {
testBluetooth();
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
。。。。。。
}
private final int REQUEST_ENABLE_BT = 1;
private void testBluetooth() {
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (mBluetoothAdapter == null) {
// Device does not support Bluetooth
Toast.makeText(getApplicationContext(), "Device does not support Bluetooth", Toast.LENGTH_LONG).show();
}
else{
//android.bluetooth.BluetoothAdapter@211120b0
Toast.makeText(getApplicationContext(), "Device support Bluetooth", Toast.LENGTH_SHORT).show();
if (!mBluetoothAdapter.isEnabled()) {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
Set pairedDevices = mBluetoothAdapter.getBondedDevices();
// If there are paired devices
if (pairedDevices.size() > 0) {
//ArrayAdapter mArrayAdapter = new ArrayAdapter();
// Loop through paired devices
for (BluetoothDevice device : pairedDevices) {
//mArrayAdapter.add(device.getName() + "\n" + device.getAddress());
Toast.makeText(getApplicationContext(), device.getName() + "\n" + device.getAddress(), Toast.LENGTH_SHORT).show();
}
}
}
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == REQUEST_ENABLE_BT)
{
if(resultCode == RESULT_OK){
Toast.makeText(getApplicationContext(), "Enabled Bluetooth now", Toast.LENGTH_LONG).show();
}
else{
Toast.makeText(getApplicationContext(), "Not enable Bluetooth !", Toast.LENGTH_LONG).show();
}
}
}
但是onActivityResult不执行。
【解决过程】
1.参考:
去试试。没太看懂。
2.看了一堆,包括:
都说是加载模式的问题。
但是去看了看,此处自己的AndroidManifest.xml中的activity中根本没有android:launchMode的配置。
估计默认就是正常的standard的。
3.参考:
中也确保了第二个参数是大于0的。
4.把测试代码testBluetooth从当前的onCreate的内部的前部移到后面了。还是不行。
5.后来的后来,确定了原因了:
当前只是把代码放在另外某个,本身就有问题的activity中,所以有问题。
换到一个正常的activity后,就可以正常显示了。
后来关于蓝牙这部分的代码为:public class ScanActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
。。。。。。
testBluetooth();
}
private final int REQUEST_ENABLE_BT = 1;
private BluetoothAdapter mBluetoothAdapter;
private void testBluetooth() {
mBluetoothAdapter= BluetoothAdapter.getDefaultAdapter();
if (mBluetoothAdapter == null) {
// Device does not support Bluetooth
Toast.makeText(getApplicationContext(), "Device does not support Bluetooth", Toast.LENGTH_LONG).show();
}
else{
//android.bluetooth.BluetoothAdapter@211120b0
Toast.makeText(getApplicationContext(), "Device support Bluetooth", Toast.LENGTH_SHORT).show();
if (!mBluetoothAdapter.isEnabled()) {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}
else{
scanBtDevices();
}
}
}
private void scanBtDevices(){
Set pairedDevices = mBluetoothAdapter.getBondedDevices(); //[BC:85:1F:96:99:C9, 00:06:66:4C:75:FE]
// If there are paired devices
if (pairedDevices.size() > 0) {
//ArrayAdapter mArrayAdapter = new ArrayAdapter();
// Loop through paired devices
for (BluetoothDevice btDev : pairedDevices) {
//mArrayAdapter.add(btDev.getName() + "\n" + btDev.getAddress());
Toast.makeText(getApplicationContext(), btDev.getName() + "\n" + btDev.getAddress(), Toast.LENGTH_LONG).show();
}
}
}
仅供参考。
【总结】
此处,确保当前的activity正常,然后onActivityResult就可以正常被执行到了。