简体   繁体   English

无法连接到蓝牙?

[英]Unable to connect to the Bluetooth?

I am trying to connect to another Bluetooth through programmatically in android but continuously I am getting this error. 我正在尝试通过编程方式在android中连接到另一个蓝牙,但不断地我收到此错误。 anyone, please help me to connect Bluetooth. 任何人,请帮助我连接蓝牙。 is there anything wrong with my code. 我的代码有什么问题吗? i am working with bluetooth from last 2 weeks but unable to connect to the bluetooth. 我从过去2个星期开始使用蓝牙,但是无法连接到蓝牙。 is there working source code availble in github it may be helpful for me github中是否有可用的工作源代码,这可能对我有帮助

Error-> BluetoothAdapter: getBluetoothService() called withBluetoothManagerCallback 错误-> BluetoothAdapter:使用BluetoothManagerCallback调用的getBluetoothService()

BluetoothAdapter mBluetoothAdapter;
BluetoothDevice mBluetoothDevice;

ListView paired_lv;
Button paired_btn, listen, sendData;
TextView deviceTxt;

ClientSocket socket;
ConnectedThread connectedThread;

private static final UUID MY_UUID_SECURE =
        UUID.fromString("fa87c0d0-afac-11de-8a39-0800200c9a66");
private static final UUID MY_UUID_INSECURE =
        UUID.fromString("8ce255c0-200a-11e0-ac64-0800200c9a66");
private static final UUID BTMODULEUUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");

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

    paired_btn = findViewById(R.id.paired_devices);
    paired_lv = findViewById(R.id.paired_lv);
    deviceTxt = findViewById(R.id.device_name);
    listen = findViewById(R.id.listen);
    sendData = findViewById(R.id.send_data);

    mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    if (mBluetoothAdapter == null){
            toast("Your device doesn't support bluetooth adapter");
    }else if (!mBluetoothAdapter.isEnabled()){
        mBluetoothAdapter.enable();
    }
    paired_btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            pairedDevices();
        }
    });

    paired_lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

            BluetoothDevice device = (BluetoothDevice) adapterView.getItemAtPosition(i);
            toast("clicket"+device.getName() );
            socket = new ClientSocket(device);
            socket.start();
        }
    });

    listen.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
           // AcceptThread acceptThread = new AcceptThread();
            //acceptThread.start();
            if (socket != null){
                socket.cancel();
            }
        }
    });
    sendData.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            String mes = "bhanu";
            Log.d("BT","Send button clicked");
            connectedThread.write("bhanu");
        }
    });

    IntentFilter filter = new IntentFilter();
    filter.addAction(BluetoothDevice.ACTION_ACL_CONNECTED);
    filter.addAction(BluetoothDevice.ACTION_ACL_DISCONNECTED);
    registerReceiver(mReceiver,filter);
}

private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if (BluetoothDevice.ACTION_ACL_CONNECTED.equals(action)){
            Log.d("BT","device connected");
            toast("device connected");
        }else if (BluetoothDevice.ACTION_ACL_CONNECTED.equals(action)){
            toast("device disconnected");
        }
    }
};
public void toast(String message){
    Toast.makeText(getApplicationContext(),message,Toast.LENGTH_SHORT).show();
}
public void pairedDevices(){
    Set<BluetoothDevice> devices = mBluetoothAdapter.getBondedDevices();
    ArrayList<BluetoothDevice> arrayList = new ArrayList<>();

    if (devices.size()<0){
        toast("no paired devices found");
    }else {

        for (BluetoothDevice device:devices){
            toast(device.getName());
            arrayList.add(device);

            ArrayAdapter<BluetoothDevice> adapter = new ArrayAdapter<>(getApplicationContext(),android.R.layout.simple_list_item_1,arrayList);
            paired_lv.setAdapter(adapter);
        }
    }


}

private class ClientSocket extends Thread{
   private final BluetoothDevice mDevice;
   private final BluetoothSocket mSocket;

    public ClientSocket(BluetoothDevice device,Boolean isSecure) {
        mDevice = device;
        BluetoothSocket tmp = null;
        boolean secure = isSecure;


            try {
                tmp = device.createInsecureRfcommSocketToServiceRecord(MY_UUID_INSECURE);
            } catch (IOException e) {
                e.printStackTrace();
            }

        mSocket = tmp;

    }

    @Override
    public void run() {
        mBluetoothAdapter.cancelDiscovery();

            if (mDevice.getBondState() == BluetoothDevice.BOND_BONDED){
                Log.d("BT","bonded");
            }
            if (BluetoothDevice.DEVICE_TYPE_LE == mDevice.getType()){

                try {
                    Log.d("BT","Connecting BT");
                    Log.d("BT","socket info"+ mSocket);
                    mSocket.connect();
                    Log.d("BT","Connected BT");
                } catch (IOException e) {
                    cancel();
                    e.printStackTrace();
                }
            }

            if (mSocket != null){

                connectedThread = new ConnectedThread(mSocket);
            }

    }
    public void cancel(){
        if (mSocket.isConnected()){
            try {
                mSocket.close();
                Log.d("BT","socket was closed");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }else {
            Log.d("BT","device not connected to the any other device");
        }
    }
}

private class AcceptThread extends Thread{
    private final BluetoothServerSocket mServerSocket;

    public AcceptThread() {
        BluetoothServerSocket tmp = null;

        try {
            tmp = mBluetoothAdapter.listenUsingInsecureRfcommWithServiceRecord("Bhanu",MY_UUID_INSECURE);

        } catch (IOException e) {
            e.printStackTrace();
        }

        mServerSocket = tmp;
    }

    @Override
    public void run() {

        BluetoothSocket socket = null;

        while (true){
            try {
                //Toast.makeText(getApplicationContext(),"ready to accept",Toast.LENGTH_SHORT).show();
                Log.d("BT","ready to accept socket");
                socket = mServerSocket.accept();
                Log.d("BT","socket accepted");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

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

}

You need a BluetoothManager 您需要一个BluetoothManager

BluetoothManager mBluetoothManager;

if (mBluetoothManager == null) {
            mBluetoothManager = (BluetoothManager) getSystemService(this);
            if (mBluetoothManager == null) {
                Log.e(TAG, "Unable to initialize BluetoothManager.");

            }
        }

        mBluetoothAdapter = mBluetoothManager.getAdapter();
        if (mBluetoothAdapter == null) {
            Log.e(TAG, "Unable to obtain a BluetoothAdapter.");

        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM