Acer Iconia One B3-A10蓝牙连接问题

时间:2017-12-05 21:24:34

标签: android bluetooth rfcomm spp

我有一个应用程序连接到使用SPP的设备,由于某种原因,我们的Acer Iconia One B3-A10将无法​​连接到此设备。我们的其他平板电脑没有连接问题。

这是我的代码:

public class BluetoothConnectionService {

    Context mContext;
    static BluetoothAdapter btAdapter;
    static ArrayList<BluetoothDevice> pairedDevices;
    static BluetoothDevice selectedDevice;

    static BluetoothSocket socket;

    static InputStream inStream;
    static OutputStream outStream;

    public static boolean isBluetoothEnabled() {
        btAdapter = BluetoothAdapter.getDefaultAdapter();
        if (btAdapter == null) {
            return false;
        } else {
            if (!btAdapter.isEnabled()) {
            // Bluetooth is not enabled :)
                return false;
            } else {
                return true;
            }
        }
    }

    public static ArrayList<BluetoothDevice> getPairedDevices() {
        btAdapter = BluetoothAdapter.getDefaultAdapter();
        Set<BluetoothDevice> devices = btAdapter.getBondedDevices();
        if (devices.size() > 0) {
            pairedDevices = new ArrayList<BluetoothDevice>();
            for (BluetoothDevice device : devices) {

                pairedDevices.add(device);
            }
        }
        return pairedDevices;
    }

    static void sendMessage(BluetoothSocket socket, String msg) {
        OutputStream outStream;
        if (socket != null) {
            try {
                outStream = socket.getOutputStream();
                outStream.write(msg.getBytes());
           } catch (IOException e) {
                Log.d("BLUETOOTH_COMMS", e.getMessage());
           }
        }
    }


    public static void setDevice(BluetoothDevice device) {
        selectedDevice = device;
    }

    public static BluetoothDevice getDevice() {
        return selectedDevice;
    }

    public static void setSocket(BluetoothSocket msocket) {
        socket = msocket;
    }

    public static void closeSocket() {
        try {
            if (socket != null) {
                socket.close();
            }
        } catch (IOException e) {
           Log.e("Error", "Could not close socket!");
        }
    }

    public static BluetoothSocket getSocket() {
        return socket;
    }

    public static void setInStream(InputStream inStream) {
        inStream = inStream;
    }

    public static void setOutStream(OutputStream outStream) {
        outStream = outStream;
    }

    public static InputStream getInStream() {
        return inStream;
    }

    public static OutputStream getOutStream() {
        return outStream;
    }
}

public abstract class ConnectTask extends AsyncTask<Void, Void, Void> {

    Context mContext;
    BluetoothDevice mdevice;
    BluetoothSocket mSocket;
    ProgressDialog pd;

    public ConnectTask(Context context) {
        mContext = context;
    }

    @Override
    protected void onPreExecute() {
    super.onPreExecute();
        if (pd !=null) {
            pd = null;
        }
        pd = new ProgressDialog(mContext);
        pd.setTitle("Connecting...");
        pd.setCancelable(false);
        if (!pd.isShowing()) {
            pd.show();
        }
    }

    @Override
    protected Void doInBackground(Void... params) {
    try {
            mdevice = BluetoothConnectionService.getDevice();
            UUID uuid = mdevice.getUuids()[0].getUuid();
            mSocket = mdevice.createRfcommSocketToServiceRecord(uuid);
            mSocket=mdevice.createInsecureRfcommSocketToServiceRecord(uuid);
            mSocket.connect();
        } catch (IOException e) {
            Log.e("Error", "Could not connect socket!");
        if (mSocket != null) {
            try {
                mSocket.close();
            } catch (IOException e1) {
                Log.e("Error", "Can't close socket!");
            }
        }

        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
    super.onPostExecute(result);
        try {
            if (pd != null) {
                if (pd.isShowing()) {
                    pd.dismiss();
                }
            }
        } catch (final IllegalArgumentException are) {
            Log.e("Error", "Illegal Argument Exception);
        } finally {
            pd = null;
        }
        BluetoothConnectionService.setSocket(mSocket);
        onComplete();
    }

    //start session
    abstract void onComplete();

    public void dismissDialog() {
        if (pd != null) {
            if (pd.isShowing()) {
                pd.dismiss();
            }
        }
        pd = null;
    }
}

有没有办法让Acer Iconia One B3-A10连接?或者是否存在阻止它的硬件限制?

0 个答案:

没有答案