查询蓝牙设备android

问题描述:

我是新来的机器人,我卡住了。我的基本前提是,当一个应用程序打开时,我需要提示用户打开蓝牙。当按下时,我需要自动开始扫描附近的设备。还有一个前提是我不应该使用任何按钮来开始扫描。它应该在蓝牙开启时自动启动。查询蓝牙设备android

该代码没有语法错误。我在逻辑上做错了什么?

因为当我运行应用程序时,什么也没有发生。它提示建立蓝牙连接,但打开后,它应该自动扫描并填充activity_main文件中的listView。这没有发生。

的activity_main.xml中:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:paddingBottom="@dimen/activity_vertical_margin" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
tools:context="com.example.shashineha.mybluetoothapp.MainActivity"> 

    <ListView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id = "@+id/listView_Bluetooth"/> 

</RelativeLayout> 

的mainActivity.java:

package com.example.bluetooth.bluetoothApp 
import android.support.v7.app.AppCompatActivity; 
import android.bluetooth.BluetoothAdapter; 
import android.bluetooth.BluetoothDevice; 
import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 
import android.content.IntentFilter; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.util.Log; 
import android.widget.ArrayAdapter; 
import android.widget.ListView; 
import com.example.shashineha.mybluetoothapp.R; 
public class MainActivity extends AppCompatActivity { 
ListView listView; 
BluetoothAdapter bluetoothAdapter; 
ArrayAdapter mArrayAdapter; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    bluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); 
    if (bluetoothAdapter != null) { 
     if (!bluetoothAdapter.isEnabled()) { 
      Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); 
      startActivityForResult(intent, 0); 
     } 
    } 
    // Register the BroadcastReceiver 
    IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND); 
    registerReceiver(mReceiver, filter); 
} 

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 
    if(bluetoothAdapter.isEnabled()) 
    { 
     bluetoothAdapter.startDiscovery(); 
    } 
} 

// Create a BroadcastReceiver for ACTION_FOUND 
final BroadcastReceiver mReceiver = new BroadcastReceiver() { 
    public void onReceive(Context context, Intent intent) { 
     listView = (ListView)findViewById(R.id.listView_Bluetooth); 
     String action = intent.getAction(); 
     // When discovery finds a device 
     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 
      mArrayAdapter.add(device.getName() + "\n" + device.getAddress()); 
      listView.setAdapter(mArrayAdapter); 
     } 
    } 
}; 


@Override 
protected void onDestroy() 
{ 
    super.onDestroy(); 
    unregisterReceiver(mReceiver); 
    bluetoothAdapter.cancelDiscovery(); 
    bluetoothAdapter.disable(); 
} 

} 

在AndroidManifest.xml中:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
package="com.example.bluetooth.mybluetoothapp"> 
<uses-permission android:name="android.permission.BLUETOOTH" /> 
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" /> 
<application 
    android:allowBackup="true" 
    android:icon="@mipmap/ic_launcher" 
    android:label="@string/app_name" 
    android:supportsRtl="true" 
    android:theme="@style/AppTheme"> 
    <activity android:name=".MainActivity"> 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 
      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 
</application> 
</manifest> 
+0

您是否在manifest中声明了适当的权限?请向我们展示您的清单。 – user6657161

+0

另请参阅https://developer.android.com/guide/topics/connectivity/bluetooth.html#Permissions – user6657161

+0

是的。我已经在清单中声明了权限。更新相同。但它仍然无法正常工作。 :( –

请确保您在添加这些权限您表现。

<uses-permission android:name="android.permission.BLUETOOTH"/> 
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/> 

在此之后,更改条件有点这样你onCreate()

if (!bluetoothAdapter.isEnabled()) { 
      Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); 
      startActivityForResult(intent, 0); 
     } else { 
      bluetoothAdapter.startDiscovery(); 
     } 

如果蓝牙已经开启,它将这是不以眼下的情况简单的开始扫描。现在它应该扫描。还要注意行

mArrayAdapter.add(device.getName() + "\n" + device.getAddress());

将再次创造零点例外中的某些设备创立。您可以首先简单地检查日志消息。绑定适配器,因为它在启动时为空。还要确保你有一些可以扫描的蓝牙设备。