这是我第一次在安康。 我是安东和贾瓦的新鲜事。 不过,我是SOS/ObjC的专家。 我正在学习。 因此,我跳了直截了当,把一个蓝色装置连接起来。 第一步当然是获得一个范围可达的蓝色装置清单。
我在此表明:
<?xml version="1.0" encoding="utf-8"?>
<manifest...>
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<application ...
我的活动守则如下:
private BluetoothAdapter btAdapter;
@Override
public void onDestroy() {
super.onDestroy();
// Unregister broadcast listeners
unregisterReceiver(mReceiver);
}
/*------------- ON CREATE ------------------------------*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btAdapter = BluetoothAdapter.getDefaultAdapter();
if (btAdapter == null) {
System.out.println ("Bluetooth non support");
} else {
System.out.println ("Bluetooth initialized");
}
IntentFilter filter = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED);
registerReceiver(mReceiver, filter);
IntentFilter filterDevice = new IntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver(mReceiver, filterDevice);
if (btAdapter.isEnabled()) {
String mydeviceaddress = btAdapter.getAddress();
String mydevicename = btAdapter.getName();
String status = mydevicename + " : " + mydeviceaddress;
System.out.println(status);
System.out.println ("Start discover");
btAdapter.startDiscovery();
} else {
System.out.println ("Not enabled");
Intent enableBT = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBT, 1);
}
}
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
final String action = intent.getAction();
if (action.equals(BluetoothAdapter.ACTION_STATE_CHANGED)) {
final int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE,
BluetoothAdapter.ERROR);
switch (state) {
case BluetoothAdapter.STATE_OFF:
System.out.println("1");
break;
case BluetoothAdapter.STATE_TURNING_OFF:
System.out.println("2");
break;
case BluetoothAdapter.STATE_ON:
System.out.println("3");
// SCAN HERE
btAdapter.startDiscovery();
break;
case BluetoothAdapter.STATE_TURNING_ON:
System.out.println("4");
break;
}
}
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
System.out.println(device.getName() + "
" + device.getAddress());
} else {
System.out.println("What de fuq");
}
}
};
我在我的和roid的电话上转 on蓝色,然后用 app显示:
Bluetooth initialized Start discover
所有这一切。 其他标志没有打印。 任何想法为什么? 我的法典似乎完美无缺。
EDIT: Screenshot of the Bluetooth module HC-05 being detected by Android.