我在手机上打开蓝牙,手机是最新版本的galaxy s8。 不明白什么我不能得到蓝牙4.0设备回电。
以下是代码段。
public class MainActivity extends AppCompatActivity {
private BluetoothLeScanner mBLEScanner;
BluetoothAdapter mBluetoothAdapter;
private static final long SCAN_PERIOD = 5000;
Handler mHandler=null;
Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
checkPermission();
BluetoothAdapter bluetoothAdapter =
BluetoothAdapter.getDefaultAdapter();
if(!bluetoothAdapter.isEnabled()){
bluetoothAdapter.enable();
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
mBLEScanner=bluetoothAdapter.getBluetoothLeScanner();
}
mHandler = new Handler();
btn=(Button)findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.e("main","now calling bluetooth low energy");
scanLeDevice(true);
}
});
}
void checkPermission(){
String[] permissions = {
Manifest.permission.INTERNET,
Manifest.permission.ACCESS_NETWORK_STATE,
Manifest.permission.BLUETOOTH_ADMIN,
Manifest.permission.BLUETOOTH,
Manifest.permission.ACCESS_COARSE_LOCATION,
Manifest.permission.ACCESS_FINE_LOCATION,
};
int permissionCheck = PackageManager.PERMISSION_GRANTED;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
requestPermissions(new String[].
{Manifest.permission.READ_PHONE_STATE}, 0);
requestPermissions(new String[]
{Manifest.permission.ACCESS_COARSE_LOCATION}, 1);
}
for (int i = 0; i < permissions.length; i++) {
permissionCheck = ContextCompat.checkSelfPermission(this,
permissions[i]);
if (permissionCheck == PackageManager.PERMISSION_DENIED) {
Log.e("main", "denied" + permissions[i]);
ActivityCompat.requestPermissions(this, permissions, i);
if
(ActivityCompat.shouldShowRequestPermissionRationale(this,
permissions[i])) {
Log.e("main", "shouldshow" + permissions[i]);
} else {
Log.e("main", "requesting" + permissions[i]);
ActivityCompat.requestPermissions(this, permissions, i);
}
break;
} else {
Log.e("main", "granted" + permissions[i]);
}
}
}
@TargetApi(21)
private void scanLeDevice(final boolean enable) {
if (enable) {
//stops scanning after a pre-defined scan period
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
if (Build.VERSION.SDK_INT < 21) {
System.out.println("BLE// mBluetoothAdapter.stopLeScan(mLeScanCallback) ");
mBluetoothAdapter.stopLeScan(mLeScanCallback);
} else {
mBLEScanner.stopScan(mScanCallback);
System.out.println("BLE// mLEScanner.stopScan(mScanCallback) ");
}
}
}, SCAN_PERIOD);
if (Build.VERSION.SDK_INT < 21) {
System.out.println("BLE// mBluetoothAdapter.startLeScan(mLeScanCallback)");
mBluetoothAdapter.startLeScan(mLeScanCallback);
} else {
Log.e("main","starting scan for second"+mBLEScanner);
mBLEScanner.startScan(mScanCallback);
//mLEScanner.startScan(filters, settings, mScanCallback);
}
} else {
if (Build.VERSION.SDK_INT < 21) {
mBluetoothAdapter.stopLeScan(mLeScanCallback);
} else {
mBLEScanner.stopScan(mScanCallback);
}
}
}
private BluetoothAdapter.LeScanCallback mLeScanCallback = new BluetoothAdapter.LeScanCallback() {
@Override
public void onLeScan(final BluetoothDevice device, int rssi,
byte[] scanRecord) {
}
};
private ScanCallback mScanCallback = new ScanCallback() {
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
public void onScanResult(int callbackType, ScanResult result) {
super.onScanResult(callbackType, result);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Log.e("main","this name : "+result.getDevice().getName());
Log.e("main","this addr : "+result.getDevice().getAddress());
}
BluetoothDevice btDevice = result.getDevice();
}
@Override
public void onBatchScanResults(List<ScanResult> results) {
}
@Override
public void onScanFailed(int errorCode) {
Log.e("main", "Error Code: " + errorCode);
}
};
}
和日志消息如下。
3:19:59.167 3251-3251 / smartiot.ica.org.blelowtest E / main:grantedandroid.permission.INTERNET 06-13 23:19:59.167 3251-3251 / smartiot.ica.org.blelowtest E / main:grantedandroid.permission.ACCESS_NETWORK_STATE 06-13 23:19:59.167 3251-3251 / smartiot.ica.org.blelowtest E / main:grantedandroid.permission.BLUETOOTH_ADMIN 06-13 23:19:59.168 3251-3251 / smartiot.ica.org.blelowtest E / main:grantedandroid.permission.BLUETOOTH 06-13 23:19:59.168 3251-3251 / smartiot.ica.org.blelowtest E / main:deniedandroid.permission.ACCESS_COARSE_LOCATION 06-13 23:19:59.171 3251-3251 / smartiot.ica.org.blelowtest E / main:requesandroid.permission.ACCESS_COARSE_LOCATION 06-13 23:20:15.188 3251-3251 / smartiot.ica.org.blelowtest E / main:现在称蓝牙低功耗 06-13 23:20:15.189 3251-3251 / smartiot.ica.org.blelowtest E / main:启动扫描secondandroid.bluetooth.le.BluetoothLeScanner@e24e0e4 06-13 23:20:15.264 3251-3251 / smartiot.ica.org.blelowtest E / main:错误代码:2
清单代码如下
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="smartiot.ica.org.mbluetooth">
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-feature android:name="android.hardware.bluetooth_le" android:required="true"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<application
答案 0 :(得分:1)
你是否在你的清单中宣布了这个,从清单中的谷歌中找到它并添加我刚刚从你的代码中复制的那个权限
如果没有在清单文件中声明,运行时权限不起作用
Manifest.permission.INTERNET,
Manifest.permission.ACCESS_NETWORK_STATE,
Manifest.permission.BLUETOOTH_ADMIN,
Manifest.permission.BLUETOOTH,
Manifest.permission.ACCESS_COARSE_LOCATION,
Manifest.permission.ACCESS_FINE_LOCATION,
答案 1 :(得分:0)
您应该在运行时请求启用蓝牙适配器:
private void sendEnableBLERequestIntent() {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}
在此处捕获startActivityForResult响应:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_ENABLE_BT && resultCode == RESULT_OK) {
// your stuff here
}
}
&#13;