为什么我的蓝牙插座没有连接?

时间:2017-06-12 16:27:50

标签: java android bluetooth android-bluetooth

我遵循this教程,但它不起作用。我的蓝牙插座没有连接。这是我的代码:

package com.levoto.hm.bt;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.Intent;
import android.os.ParcelUuid;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import java.io.IOException;
import java.sql.Time;
import java.util.Set;
import java.util.UUID;

public class MainActivity extends AppCompatActivity {

    private BluetoothAdapter bluetoothAdapter;
    private BluetoothDevice bluetoothLED;
    private ImageView imageView;
    private TextView notification;
    private Button ON;
    private Button OFF;
    private BluetoothSocket mmsoket;
    private boolean flag = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        imageView = (ImageView) findViewById(R.id.imageView);
        notification = (TextView) findViewById(R.id.notification_view);
        ON = (Button) findViewById(R.id.buttonON);
        OFF = (Button) findViewById(R.id.buttonOFF);
        bluetoothLED = null;




        bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        if(bluetoothAdapter == null) {
            makeMsg("No adapter found.");
            finish();
        }

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

        Set<BluetoothDevice> deviceSet = bluetoothAdapter.getBondedDevices();

        if (deviceSet.size() == 0) {
            makeMsg("No paired device found.\n Please pair first");
            finish();
        }

        for(BluetoothDevice device : deviceSet) {
            if(device.getName().equals("HM LED")) {
                makeMsg("Bluetooth led found");
                bluetoothLED = device;
                break;
            }
        }

        if(bluetoothLED == null) {
            makeMsg("BT LED is not found among the paired devices please");
            finish();
        }

        makeNoitification("Starting connection.");
        mmsoket = null;
        int count = 1;

        while(mmsoket == null) {
            try{
                mmsoket = bluetoothLED.createInsecureRfcommSocketToServiceRecord(UUID.randomUUID());
                makeNoitification("Connecting");
                mmsoket.connect();
                makeNoitification("Connected!");
            } catch (IOException e) {
                makeNoitification("Failed to connect.");
                try {
                    mmsoket.close();
                    mmsoket = null;
                } catch (IOException e1) {
                    mmsoket = null;
                }
            }
            count++;
            if(count == 10) {
                flag = false;
                break;
            }
        }

        if (flag){

        }



        if(flag){

        ON.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()){
                    case MotionEvent.ACTION_DOWN:
                        ON.setAlpha(.5f);
                        break;
                    case MotionEvent.ACTION_UP:
                        ON.setAlpha(1f);
                        imageView.setImageResource(R.drawable.on_ic);
                        makeNoitification("Led turned on.");
                        break;
                    default:
                        break;
                }
                return false;
            }
        });

        OFF.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()){
                    case MotionEvent.ACTION_DOWN:
                        OFF.setAlpha(.5f);
                        break;
                    case MotionEvent.ACTION_UP:
                        OFF.setAlpha(1f);
                        imageView.setImageResource(R.drawable.off_ic);
                        makeNoitification("Led turned off.");
                        break;
                    default:
                        break;
                }
                return false;
            }
        });}else  {
            OFF.setAlpha(.5f);
            ON.setAlpha(.5f);
        }

    }

    private  void  makeMsg(String s) {
        Toast.makeText(this,s,Toast.LENGTH_SHORT).show();
    }

    private void makeNoitification(String s) {
        notification.setText(String.format(s));
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

}

当我使用上面的代码时,套接字没有连接,但程序有效,但如果我更改了行:

bluetoothLED.createInsecureRfcommSocketToServiceRecord(UUID.randomUUID());

要:

bluetoothLED.createInsecureRfcommSocketToServiceRecord(UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");

该计划不再响应。

LogCat显示:

 06-13 21:37:46.167 31812-31812/? I/art: Late-enabling -Xcheck:jni
06-13 21:37:46.311 31812-31812/com.levoto.hm.btled W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg  --debuggable --instruction-set=arm64 --instruction-set-features=smp,a53 --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=generic --instruction-set-features=default --dex-file=/data/app/com.levoto.hm.btled-1/split_lib_dependencies_apk.apk --oat-file=/data/dalvik-cache/arm64/data@app@com.levoto.hm.btled-1@split_lib_dependencies_apk.apk@classes.dex) because non-0 exit status
06-13 21:37:46.519 31812-31812/com.levoto.hm.btled W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg  --debuggable --instruction-set=arm64 --instruction-set-features=smp,a53 --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=generic --instruction-set-features=default --dex-file=/data/app/com.levoto.hm.btled-1/split_lib_slice_0_apk.apk --oat-file=/data/dalvik-cache/arm64/data@app@com.levoto.hm.btled-1@split_lib_slice_0_apk.apk@classes.dex) because non-0 exit status
06-13 21:37:46.568 31812-31812/com.levoto.hm.btled W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg  --debuggable --instruction-set=arm64 --instruction-set-features=smp,a53 --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=generic --instruction-set-features=default --dex-file=/data/app/com.levoto.hm.btled-1/split_lib_slice_1_apk.apk --oat-file=/data/dalvik-cache/arm64/data@app@com.levoto.hm.btled-1@split_lib_slice_1_apk.apk@classes.dex) because non-0 exit status
06-13 21:37:46.612 31812-31812/com.levoto.hm.btled W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg  --debuggable --instruction-set=arm64 --instruction-set-features=smp,a53 --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=generic --instruction-set-features=default --dex-file=/data/app/com.levoto.hm.btled-1/split_lib_slice_2_apk.apk --oat-file=/data/dalvik-cache/arm64/data@app@com.levoto.hm.btled-1@split_lib_slice_2_apk.apk@classes.dex) because non-0 exit status
06-13 21:37:46.657 31812-31812/com.levoto.hm.btled W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg  --debuggable --instruction-set=arm64 --instruction-set-features=smp,a53 --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=generic --instruction-set-features=default --dex-file=/data/app/com.levoto.hm.btled-1/split_lib_slice_3_apk.apk --oat-file=/data/dalvik-cache/arm64/data@app@com.levoto.hm.btled-1@split_lib_slice_3_apk.apk@classes.dex) because non-0 exit status
06-13 21:37:46.699 31812-31812/com.levoto.hm.btled W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg  --debuggable --instruction-set=arm64 --instruction-set-features=smp,a53 --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=generic --instruction-set-features=default --dex-file=/data/app/com.levoto.hm.btled-1/split_lib_slice_4_apk.apk --oat-file=/data/dalvik-cache/arm64/data@app@com.levoto.hm.btled-1@split_lib_slice_4_apk.apk@classes.dex) because non-0 exit status
06-13 21:37:46.741 31812-31812/com.levoto.hm.btled W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg  --debuggable --instruction-set=arm64 --instruction-set-features=smp,a53 --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=generic --instruction-set-features=default --dex-file=/data/app/com.levoto.hm.btled-1/split_lib_slice_5_apk.apk --oat-file=/data/dalvik-cache/arm64/data@app@com.levoto.hm.btled-1@split_lib_slice_5_apk.apk@classes.dex) because non-0 exit status
06-13 21:37:46.783 31812-31812/com.levoto.hm.btled W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg  --debuggable --instruction-set=arm64 --instruction-set-features=smp,a53 --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=generic --instruction-set-features=default --dex-file=/data/app/com.levoto.hm.btled-1/split_lib_slice_6_apk.apk --oat-file=/data/dalvik-cache/arm64/data@app@com.levoto.hm.btled-1@split_lib_slice_6_apk.apk@classes.dex) because non-0 exit status
06-13 21:37:46.824 31812-31812/com.levoto.hm.btled W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg  --debuggable --instruction-set=arm64 --instruction-set-features=smp,a53 --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=generic --instruction-set-features=default --dex-file=/data/app/com.levoto.hm.btled-1/split_lib_slice_7_apk.apk --oat-file=/data/dalvik-cache/arm64/data@app@com.levoto.hm.btled-1@split_lib_slice_7_apk.apk@classes.dex) because non-0 exit status
06-13 21:37:46.865 31812-31812/com.levoto.hm.btled W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg  --debuggable --instruction-set=arm64 --instruction-set-features=smp,a53 --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=generic --instruction-set-features=default --dex-file=/data/app/com.levoto.hm.btled-1/split_lib_slice_8_apk.apk --oat-file=/data/dalvik-cache/arm64/data@app@com.levoto.hm.btled-1@split_lib_slice_8_apk.apk@classes.dex) because non-0 exit status
06-13 21:37:46.906 31812-31812/com.levoto.hm.btled W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg  --debuggable --instruction-set=arm64 --instruction-set-features=smp,a53 --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=generic --instruction-set-features=default --dex-file=/data/app/com.levoto.hm.btled-1/split_lib_slice_9_apk.apk --oat-file=/data/dalvik-cache/arm64/data@app@com.levoto.hm.btled-1@split_lib_slice_9_apk.apk@classes.dex) because non-0 exit status
06-13 21:37:46.908 31812-31812/com.levoto.hm.btled W/System: ClassLoader referenced unknown path: /data/app/com.levoto.hm.btled-1/lib/arm64
06-13 21:37:46.913 31812-31812/com.levoto.hm.btled I/InstantRun: starting instant run server: is main process
06-13 21:37:47.226 31812-31812/com.levoto.hm.btled I/Process: Sending signal. PID: 31812 SIG: 9

0 个答案:

没有答案