如何通过蓝牙连接电视(REMOTE)连接配对设备?

时间:2016-09-07 11:51:31

标签: android sockets bluetooth television

我的代码

mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    if (mBluetoothAdapter != null) {
        // Device does not support Bluetooth

        if (!mBluetoothAdapter.isEnabled()) {
            Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
            startActivityForResult(enableBtIntent, 10);
        }else{
            Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
            // If there are paired devices
            if (pairedDevices.size() > 0) {
                // Loop through paired devices
                for (final BluetoothDevice device : pairedDevices) {
                    // Add the name and address to an array adapter to show in a ListView
                    //mArrayAdapter.add(device.getName() + "\n" + device.getAddress());
                    devName = device.getName();
                    devAdd = device.getAddress();

                    if(!devName.isEmpty())
                    {
                        txtDesc.setText(devName);
                        progressBar.setVisibility(View.GONE);
                    }
                    txtDesc.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            //Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                            //startActivityForResult(cameraIntent, 2);
                            new ConnectThread(device);

                        }
                    });
                }

            }

用于呼叫ConnectedThread

private class ConnectThread extends Thread {
    private  BluetoothSocket mmSocket;
    public final BluetoothDevice mmDevice;

    public ConnectThread(BluetoothDevice device) {
        // Use a temporary object that is later assigned to mmSocket,
        // because mmSocket is final
        BluetoothSocket tmp = null;
        mmDevice = device;

        // Get a BluetoothSocket to connect with the given BluetoothDevice
        try {
            UUID MY_UUID = mmDevice.getUuids()[0].getUuid();
            // MY_UUID is the app's UUID string, also used by the server code
            tmp = device.createInsecureRfcommSocketToServiceRecord(MY_UUID);
            mmSocket = tmp;
            mBluetoothAdapter.cancelDiscovery();
            if(!mmSocket.isConnected()){
                Log.e("TAG" , "Connection started.");
                mmSocket.connect();
                Log.e("TAG", "Connection ended.");
            }


        } catch (IOException e)
        {
            Log.e("EXC" ,""+ e.getMessage());
            Toast.makeText(getApplicationContext() , "Unable to connect." , Toast.LENGTH_LONG).show();

            try {
                mmSocket.close();
            } catch (IOException closeException) { }
            return;
        }
    }

所以我使用了这段代码,我之前配对了设备,当我点击我将要连接的特定设备时,显示:

E/TAG: Connection started.
09-10 17:04:29.770 9565-9565/ir.abc.com.irremote D/BluetoothUtils: isSocketAllowedBySecurityPolicy start : device null
09-10 17:04:29.770 9565-9565/ir.abc.com.irremote D/BluetoothSocket: connect(): myUserId = 0
09-10 17:04:29.770 9565-9565/ir.abc.com.irremote W/BluetoothAdapter: getBluetoothService() called with no BluetoothManagerCallback
09-10 17:04:29.780 9565-9565/ir.abc.com.irremote D/BluetoothSocket: connect(), SocketState: INIT, mPfd: {ParcelFileDescriptor: FileDescriptor[40]}
09-10 17:04:36.205 9565-9565/ir.abc.com.irremote E/EXC: read failed, socket might closed or timeout, read ret: -1
09-10 17:04:36.245 9565-9565/ir.abc.com.irremote D/BluetoothSocket: close() in, this: android.bluetooth.BluetoothSocket@bb242a6, channel: -1, state: INIT
09-10 17:04:36.245 9565-9565/ir.dumadu.com.irremote D/BluetoothSocket: close() this: android.bluetooth.BluetoothSocket@bb242a6, channel: -1, mSocketIS: android.net.LocalSocketImpl$SocketInputStream@3d9924e7, mSocketOS: android.net.LocalSocketImpl$SocketOutputStream@151d4894mSocket: android.net.LocalSocket@200d2f3d impl:android.net.LocalSocketImpl@302fe332 fd:FileDescriptor[40]
09-10 17:04:36.245 9565-9565/ir.abc.com.irremote D/BluetoothSocket: Closing mSocket: android.net.LocalSocket@200d2f3d impl:android.net.LocalSocketImpl@302fe332 fd:FileDescriptor[40]
09-10 17:04:36.245 9565-9565/ir.abc.com.irremote I/Choreographer: Skipped 389 frames!  The application may be doing too much work on its main thread.
09-10 17:04:36.250 9565-9565/ir.abc.com.irremote D/ViewRootImpl: Buffer Count from app info with  ::-1 && -1 for :: ir.abc.com.irremote from View :: -1 DBQ Enabled ::false false
09-10 17:04:36.280 9565-9600/ir.abc.com.irremote D/mali_winsys: new_window_surface returns 0x3000,  [265x78]-format:1
09-10 17:14:40.345 9565-9565/ir.dumadu.com.irremote V/ActivityThread: updateVisibility : ActivityRecord{fd05a00 token=android.os.BinderProxy@2ba635fc {ir.abc.com.irremote/ir.abc.com.irremote.ir.abc.com.irremote.activity.WifiConnection}} show : true

0 个答案:

没有答案