Android 蓝牙发现无法正常工作
我是android新手。 我正在尝试一个简单的android应用程序,它将: -用户单击按钮以启用BT -用户点击按钮,它将扫描附近的设备,并给出一个列表或尝试连接到特定的设备(应用程序将用于机器人) anble按钮工作正常并启用蓝牙功能,但当我按下扫描按钮时,它会启动discover,但没有返回任何回调。我已经设置了一个监听器,并将一些日志放在那里,但我没有从监听器中看到任何东西(我确信在测试时周围有可发现的设备) 代码如下:Android 蓝牙发现无法正常工作,android,bluetooth,Android,Bluetooth,我是android新手。 我正在尝试一个简单的android应用程序,它将: -用户单击按钮以启用BT -用户点击按钮,它将扫描附近的设备,并给出一个列表或尝试连接到特定的设备(应用程序将用于机器人) anble按钮工作正常并启用蓝牙功能,但当我按下扫描按钮时,它会启动discover,但没有返回任何回调。我已经设置了一个监听器,并将一些日志放在那里,但我没有从监听器中看到任何东西(我确信在测试时周围有可发现的设备) 代码如下: public class MainActivity ext
public class MainActivity extends AppCompatActivity {
private BluetoothAdapter BA;
private Set<BluetoothDevice> scannedDevices;
private final static int REQUEST_ENABLE_BT = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver(mReceiver, filter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
protected void onDestroy() {
super.onDestroy();
BA.cancelDiscovery();
// Don't forget to unregister the ACTION_FOUND receiver.
unregisterReceiver(mReceiver);
}
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent)
{
Log.i("NKNKNK","onReceive method");
String action = intent.getAction();
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
// A Bluetooth device was found
// Getting device information from the intent
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
Log.i("NKNKNK","Device found: " + device.getName() + "; MAC " + device.getAddress());
}
}
};
public void TunrONOnClick(View v)
{
// do something when the button is clicked
Log.i("NKNKNK","TunrON button clicked");
TextView output = findViewById(R.id.textView);
BA = BluetoothAdapter.getDefaultAdapter();
if (BA == null)
{
Log.i("NKNKNK","No BT device supported");
return;
}
if (!BA.isEnabled()) {
// We need to enable the Bluetooth, so we ask the user
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
// REQUEST_ENABLE_BT es un valor entero que vale 1
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}
output.setText(BA.getName());
}
public void ScanOnClick(View v)
{
Log.i("NKNKNK","SCAN button clicked");
BA = BluetoothAdapter.getDefaultAdapter();
if (BA.isEnabled())
{
if (BA.isDiscovering()) {
BA.cancelDiscovery();
}
Log.i("NKNKNK","Starting discovery logged");
BA.startDiscovery();
}
}
}
编辑:我正在使用redmi note 4运行应用程序
谢谢,嗨,我找到了解决我问题的方法,看来从安卓6开始,你必须申请许可,而不仅仅是在清单中声明它们。因此,在启用BT之前,我必须添加以下行:
if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_COARSE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
Log.i("NKNKNK","Permission is not granted");
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, MY_PERMISSIONS_REQUEST_READ_CONTACTS);
}
那么我的权限、请求、阅读、联系人是什么?
if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_COARSE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
Log.i("NKNKNK","Permission is not granted");
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, MY_PERMISSIONS_REQUEST_READ_CONTACTS);
}