我尝试使用蓝牙在Android中创建简单的应用程序。我找到设备时遇到了问题。
我有相同的代码,例如教程:https://www.youtube.com/watch?v=hv_-tX1VwXE&index=3&list=PLgCYzUzKIBE8KHMzpp6JITZ2JxTgWqDH2 并运行本教程中的示例(源代码)。 我查看了我的Android Manifest和MainActivity。我没有更多。
我的机器人清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.user.bluetooth_discoverdevices">
<uses-feature android:name="android.hardware.bluetooth" />
<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"/>
<uses-permission android:name="android.permission.BLUETOOTH_PRIVILEGED" />
<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>
我在MainActivity中的源代码:
public class MainActivity extends AppCompatActivity {
BluetoothAdapter bluetoothAdapter;
private Button mFind;
private Switch mBluetooth;
public ArrayList<BluetoothDevice> mBTDevices = new ArrayList<>();
public DeviceListAdapter mdeviceListAdapter;
ListView lnewDevices;
private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
final String action = intent.getAction();
Log.d("MainActivity", "RUNNING-5");
if (action.equals(BluetoothDevice.ACTION_FOUND)){
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
mBTDevices.add(device);
Log.d("MainActivity", "Device " + device.getName());
Log.d("MainActivity", "RUNNING-6");
mdeviceListAdapter = new DeviceListAdapter(context,R.layout.device_list_adapter_view, mBTDevices);
lnewDevices.setAdapter(mdeviceListAdapter);
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
mBluetooth.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mBluetooth.isChecked())
{
RunBluetooth();}
if (!mBluetooth.isChecked())
{
Log.d("MainActivity","I run off");
}
}
});
mFind = (Button) findViewById(R.id.mFind);
mFind.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DiscoverDevice();
}
});
}
public void RunBluetooth()
{
if(!bluetoothAdapter.isEnabled())
{
Log.d("MainActivity", "run-1");
Intent enableBTIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBTIntent,1);
Log.d("MainActivity", "run-2");
}
if(bluetoothAdapter.isEnabled())
{
DiscoverDevice();
}
}
public void DiscoverDevice(){
Log.d("MainActivity", "RUNNING-1");
if (bluetoothAdapter.isDiscovering()){
Log.d("MainActivity", "RUNNING-2");
bluetoothAdapter.cancelDiscovery();
bluetoothAdapter.startDiscovery();
IntentFilter discoverDevicesIntent = new IntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver(mBroadcastReceiver, discoverDevicesIntent);
}
if (!bluetoothAdapter.isDiscovering())
{
Log.d("MainActivity", "RUNNING-3");
bluetoothAdapter.startDiscovery();
IntentFilter discoverDevicesIntent = new IntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver(mBroadcastReceiver, discoverDevicesIntent);
Log.d("MainActivity", "RUNNING-4");
}
}}
如您所见 - 我在应用程序中有原始调试器。当我运行app并单击按钮查找时,在我的Logcat中是:
05-27 15:22:06.005 9021-9021/com.example.kacper.kaccar D/MainActivity: RUNNING-1
05-27 15:22:06.016 9021-9021/com.example.kacper.kaccar D/MainActivity: RUNNING-3
05-27 15:22:06.025 9021-9021/com.example.kacper.kaccar D/MainActivity: RUNNING-4
我尝试添加:
IntentFilter discoverDevicesIntent = new IntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver(mBroadcastReceiver3, discoverDevicesIntent);
到protected void onCreate,但我只有
05-27 14:22:06.025 9021-9021/com.example.kacper.kaccar D/MainActivity: RUNNING-5
答案 0 :(得分:0)
在设置onClickListener之前,您需要在onCreate中初始化mBluetooth开关。 您应该在onCreate()中的setContentView()之后注册Receiver,并在onDestroy()中取消注册Receiver。