无法在bluetoothle回调中使用intent启动活动

时间:2015-10-14 05:27:21

标签: android

我无法使用这行代码启动活动

Intent i = new Intent(getApplicationContext(),MainPage.class);
                            startActivity(i);

在Bluetooth le scan回调中,我确认我已满足if语句的要求。

private BluetoothAdapter.LeScanCallback mLeScanCallback = new BluetoothAdapter.LeScanCallback() {


        public void onLeScan(final BluetoothDevice device, final int rssi, final byte[] scanRecord) {
            x++;
            int startByte = 2;
            boolean patternFound = false;
            while (startByte <= 5) {

                if (((int) scanRecord[startByte + 2] & 0xff) == 0x02 && //Identifies an iBeacon
                        ((int) scanRecord[startByte + 3] & 0xff) == 0x15) { //Identifies correct data length
                    patternFound = true;
                    break;
                }
                startByte++;
            }

            if (patternFound) {

                //Convert to hex String
                byte[] uuidBytes = new byte[16];
                System.arraycopy(scanRecord, startByte + 4, uuidBytes, 0, 16);
                String hexString = bytesToHex(uuidBytes);

                //Here is your UUID
                String uuid = hexString.substring(0, 8) + "-" +
                        hexString.substring(8, 12) + "-" +
                        hexString.substring(12, 16) + "-" +
                        hexString.substring(16, 20) + "-" +
                        hexString.substring(20, 32);

                //Here is your Major value
                major = (scanRecord[startByte + 20] & 0xff) * 0x100 + (scanRecord[startByte + 21] & 0xff);

                //Here is your Minor value
                minor = (scanRecord[startByte + 22] & 0xff) * 0x100 + (scanRecord[startByte + 23] & 0xff);
                rs = rssi;
                if (i == 0){

                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            if (rs > -55 && z==1) {

                                BluetoothGatt mBluetoothGatt = device.connectGatt(getApplication(), false, mGattCallback);


                            }


                        }


                    });
                }


                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (major!=22220 && p==0) {


                            Intent i = new Intent(getApplicationContext(),MainPage.class);
                            startActivity(i);
                            finish();
                            p=1;                            }


                    }


                });
            }

        }
    }


10-14 13:45:54.658    2402-2402/com.example.window8.rescape E/WindowManager﹕ android.view.WindowLeaked: Activity com.example.window8.rescape.Escape has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{429db1c8 V.E..... R.....ID 0,0-684,260} that was originally added here
        at android.view.ViewRootImpl.<init>(ViewRootImpl.java:468)
        at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:267)
        at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
        at android.app.Dialog.show(Dialog.java:289)
        at com.example.window8.rescape.Escape.dialogstart(Escape.java:369)
        at com.example.window8.rescape.Escape.checkfire1(Escape.java:564)
        at com.example.window8.rescape.Escape.checkposition1(Escape.java:509)
        at com.example.window8.rescape.Escape.checkpicture(Escape.java:472)
        at com.example.window8.rescape.Escape$1$1.run(Escape.java:284)
        at android.os.Handler.handleCallback(Handler.java:733)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:146)
        at android.app.ActivityThread.main(ActivityThread.java:5653)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107)
        at dalvik.system.NativeStart.main(Native Method)

0 个答案:

没有答案