我对Android很新,我正在构建我的第一个应用程序。 到目前为止,我的所有活动都是静态的,我可以实现它们 OnCreate方法。但是,当我尝试使用startDiscovery()时,我遇到了一个问题。用于蓝牙。每当我在onCreate方法中调用startDiscovery()时,页面都会崩溃。我想了一会儿并得出结论它崩溃了,因为startDiscovery()是异步的,这是停止程序创建页面,因为必须完成onCreate方法才能使程序正确显示页面。
我真的很感激有关的任何帮助:
编辑:这是我的代码的剥离版本:
public class TrashcanOnWayActivity extends ActionBarActivity {
int REQUEST_ENABLE_BT = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_trashcan_on_way);
// ==================================== Bluetooth ====================================
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
final TextView bluelist = (TextView)findViewById(R.id.list1);
// Turn on Bluetooth
if (!mBluetoothAdapter.isEnabled()) {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}
if (mBluetoothAdapter.isDiscovering()) {
mBluetoothAdapter.cancelDiscovery();
}
boolean test = false;
test = mBluetoothAdapter.startDiscovery();
if (test == true)
{
bluelist.setText("found one" );
}
else
{
bluelist.setText("didn't find one" );
}
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();}
}
这是我的logcat:
04-21 15:48:12.496: I/ActivityManager(430): Displayed com.example.trash2/.LocationPaperActivity: +150ms
04-21 15:48:14.126: I/ActivityManager(430): START u0 {cmp=com.example.trash2/.TrashcanOnWayActivity} from pid 10470
04-21 15:48:14.176: D/BluetoothManagerService(430): Message: 20
04-21 15:48:14.176: D/BluetoothManagerService(430): Added callback: android.bluetooth.IBluetoothManagerCallback$Stub$Proxy@4261b200:true
04-21 15:48:14.176: D/AndroidRuntime(10470): Shutting down VM
04-21 15:48:14.176: W/dalvikvm(10470): threadid=1: thread exiting with uncaught exception (group=0x41974700)
04-21 15:48:14.186: E/AndroidRuntime(10470): FATAL EXCEPTION: main
04-21 15:48:14.186: E/AndroidRuntime(10470): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.trash2/com.example.trash2.TrashcanOnWayActivity}: java.lang.SecurityException: Need BLUETOOTH ADMIN permission: Neither user 10072 nor current process has android.permission.BLUETOOTH_ADMIN.
04-21 15:48:14.186: E/AndroidRuntime(10470): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
04-21 15:48:14.186: E/AndroidRuntime(10470): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
04-21 15:48:14.186: E/AndroidRuntime(10470): at android.app.ActivityThread.access$600(ActivityThread.java:141)
04-21 15:48:14.186: E/AndroidRuntime(10470): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
04-21 15:48:14.186: E/AndroidRuntime(10470): at android.os.Handler.dispatchMessage(Handler.java:99)
04-21 15:48:14.186: E/AndroidRuntime(10470): at android.os.Looper.loop(Looper.java:137)
04-21 15:48:14.186: E/AndroidRuntime(10470): at android.app.ActivityThread.main(ActivityThread.java:5103)
04-21 15:48:14.186: E/AndroidRuntime(10470): at java.lang.reflect.Method.invokeNative(Native Method)
04-21 15:48:14.186: E/AndroidRuntime(10470): at java.lang.reflect.Method.invoke(Method.java:525)
04-21 15:48:14.186: E/AndroidRuntime(10470): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
04-21 15:48:14.186: E/AndroidRuntime(10470): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
04-21 15:48:14.186: E/AndroidRuntime(10470): at dalvik.system.NativeStart.main(Native Method)
04-21 15:48:14.186: E/AndroidRuntime(10470): Caused by: java.lang.SecurityException: Need BLUETOOTH ADMIN permission: Neither user 10072 nor current process has android.permission.BLUETOOTH_ADMIN.
04-21 15:48:14.186: E/AndroidRuntime(10470): at android.os.Parcel.readException(Parcel.java:1431)
04-21 15:48:14.186: E/AndroidRuntime(10470): at android.os.Parcel.readException(Parcel.java:1385)
04-21 15:48:14.186: E/AndroidRuntime(10470): at android.bluetooth.IBluetooth$Stub$Proxy.startDiscovery(IBluetooth.java:790)
04-21 15:48:14.186: E/AndroidRuntime(10470): at android.bluetooth.BluetoothAdapter.startDiscovery(BluetoothAdapter.java:770)
04-21 15:48:14.186: E/AndroidRuntime(10470): at com.example.trash2.TrashcanOnWayActivity.onCreate(TrashcanOnWayActivity.java:89)
04-21 15:48:14.186: E/AndroidRuntime(10470): at android.app.Activity.performCreate(Activity.java:5133)
04-21 15:48:14.186: E/AndroidRuntime(10470): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
04-21 15:48:14.186: E/AndroidRuntime(10470): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
04-21 15:48:14.186: E/AndroidRuntime(10470): ... 11 more
04-21 15:48:14.196: W/ActivityManager(430): Force finishing activity com.example.trash2/.TrashcanOnWayActivity
04-21 15:48:14.196: W/ActivityManager(430): Force finishing activity com.example.trash2/.LocationPaperActivity
04-21 15:48:14.286: D/dalvikvm(430): GC_CONCURRENT freed 4373K, 29% free 23450K/33004K, paused 3ms+6ms, total 74ms
04-21 15:48:14.696: W/ActivityManager(430): Activity pause timeout for ActivityRecord{423f0138 u0 com.example.trash2/.TrashcanOnWayActivity}
04-21 15:48:24.146: W/ActivityManager(430): Launch timeout has expired, giving up wake lock!
04-21 15:48:24.146: W/ActivityManager(430): Activity idle timeout for ActivityRecord{423f0138 u0 com.example.trash2/.TrashcanOnWayActivity}
04-21 15:48:24.696: W/ActivityManager(430): Activity idle timeout for ActivityRecord{420fd358 u0 com.example.trash2/.MainActivity}
04-21 15:48:34.166: W/ActivityManager(430): Activity destroy timeout for ActivityRecord{423b9698 u0 com.example.trash2/.LocationPaperActivity}
04-21 15:48:34.166: W/ActivityManager(430): Activity destroy timeout for ActivityRecord{423f0138 u0 com.example.trash2/.TrashcanOnWayActivity}
提前致谢
答案 0 :(得分:0)
您的日志表明您没有蓝牙权限。确保你在清单中写错了。需要成为下一个
<uses-feature android:name="android.hardware.bluetooth" />
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
还要确保将权限放在清单中的错误位置。你“必须”在清单的根级别写入它,并在任何其他标记内写入“NOT”。