我是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 :(得分:0)
请确保您已在清单中添加了这些权限。
Sub test()
Dim rngsize As Range, rngsize2 As Range, rngmake As Range, rngmake2 As Range, rngprice As Range, rngprice2 as range, i As Integer, j As Integer, x As Integer
x = 3
For i = 2 To Sheets("Sheet3").Range("E" & Rows.Count).End(xlUp).Row
For j = 7 To Sheets("Sheet2").Range("E" & Rows.Count).End(xlUp).Row
Set rngsize = Sheets("Sheet3").Range("E" & i)
Set rngsize2 = Sheets("Sheet2").Range("E" & j)
Set rngmake = Sheets("Sheet3").Range("F" & i)
Set rngmake2 = Sheets("Sheet2").Range("F" & j)
Set rngprice = Sheets("Sheet3").Range("X" & i)
Set rngprice2 = Sheets("Sheet2").Range("X" & j)
If rngsize * 0.5 <= rngsize And rngsize2 + 1.5 >= rngsize Then
If rngmake2 * 0.5 <= rngmake And rngmake2 * 1.5 >= rngmake Then
Range(Cells(Selection.Row, 1), Cells(Selection.Row, 3)).Copy
rngprice2.Copy
Worksheets("Sheet4").Range("F" & x).PasteSpecial Paste:=xlPasteValues
Application.CutCopyMode = xlCopy
x = x + 1
End If
End If
Next j
Next i
End Sub
在此之后,在<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();
}
将创建空点异常。您最初可以先查看日志消息。绑定适配器,因为它在启动时为null。还要确保你有一些可以扫描的蓝牙设备。