简体   繁体   English

如何在Android中连接配对蓝牙设备程序化?

[英]How to connect with paired Bluetooth device programmatic in Android?

I am developing an application where I have to connect to Bluetooth paired device. 我正在开发一个应用程序,我必须连接到蓝牙配对设备。 These two devices are paired with each other. 这两个设备相互配对。 Now when I try to connect with paired device it stuck with the connecting mode dialog. 现在,当我尝试连接配对设备时,它会停留在连接模式对话框中。 Log shows that it successfully connect but does not dismiss the dialog. 日志显示它已成功连接但未关闭对话框。

Here is my code snippet. 这是我的代码片段。

Main.java Main.java

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Random;
import java.util.Set;
import java.util.TimeZone;
import java.util.UUID;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.PorterDuff.Mode;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Environment;
import android.preference.PreferenceManager;
import android.provider.Settings;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnTouchListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class Main extends Activity 
{
    protected static final String TAG = "TAG";
    private static final int REQUEST_CONNECT_DEVICE = 1;
    private static final int REQUEST_ENABLE_BT = 2;

    Button mScan;
    BluetoothAdapter mBluetoothAdapter;
    private UUID applicationUUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");

    @Override
    public void onCreate(Bundle mSavedInstanceState) 
    {
        super.onCreate(mSavedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.main);
        mScan = (Button) findViewById(R.id.Scan);
        mScan.setOnClickListener(new View.OnClickListener() 
        {
            public void onClick(View mView) 
            {
                mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
                if (mBluetoothAdapter == null) 
                {
                    Toast.makeText(Main.this, "DeviceHasNoSupport", 2000).show();
                } 
                else 
                {
                    if (!mBluetoothAdapter.isEnabled()) 
                    {
                         Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
                        startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
                     } 
                     else 
                     {
                        ListPairedDevices();
                        Intent connectIntent = new Intent(Main.this, DeviceListActivity.class);
                        startActivityForResult(connectIntent, REQUEST_CONNECT_DEVICE);
                    }
                }
            }
        });
    }// onCreate

    public void onActivityResult(int mRequestCode, int mResultCode, Intent mDataIntent) 
    {
        super.onActivityResult(mRequestCode, mResultCode, mDataIntent);

        switch (mRequestCode) 
        {
            case REQUEST_CONNECT_DEVICE:
                if (mResultCode == Activity.RESULT_OK) 
                {
                    Bundle mExtra = mDataIntent.getExtras();
                    String mDeviceAddress = mExtra.getString("DeviceAddress");
                    Log.v(TAG, "Coming incoming address " + mDeviceAddress);
                    BluetoothDevice mBluetoothDevice = mBluetoothAdapter.getRemoteDevice(mDeviceAddress);
                    pairToDevice(mBluetoothDevice);
                    Log.v(TAG, "pairToDeviceCalled");
                }
                break;

            case REQUEST_ENABLE_BT:
                if (mResultCode == Activity.RESULT_OK) 
                {
                    ListPairedDevices();
                    Intent connectIntent = new Intent(Main.this, DeviceListActivity.class);
                    startActivityForResult(connectIntent, REQUEST_CONNECT_DEVICE);
                } 
                else 
                {
                    Toast.makeText(Main.this, "BTNotEnabled", 2000).show();
                }
                break;
        }
    }

    private void ListPairedDevices() 
    {
        Set<BluetoothDevice> mPairedDevices = mBluetoothAdapter.getBondedDevices();
        if (mPairedDevices.size() > 0) 
        {
            for (BluetoothDevice mDevice : mPairedDevices) 
            {
                Log.v(TAG, "PairedDevices: " + mDevice.getName() + " " + mDevice.getAddress());
            }
        }
    }

    private void pairToDevice(BluetoothDevice nBluetoothDevice) 
    {
        Log.v(TAG, "InsidepairToDeviceCalled");
        openSocket(nBluetoothDevice);
        Log.v(TAG, "LeavingpairToDeviceCalled");
    }

    private void openSocket(BluetoothDevice nBluetoothDevice) 
    {
        try 
        {
            Log.v(TAG, "InsideOpenSockedCalled");
            final ProgressDialog dialog = new ProgressDialog(this);
            final ConnectRunnable connector = new ConnectRunnable(nBluetoothDevice, dialog);
            Log.v(TAG, "InsideOpenSockedConnecterCalled");
            ProgressDialog.show(this, "Connecting...", nBluetoothDevice.getName() + " : " + nBluetoothDevice.getAddress(),
                true, true,
                new OnCancelListener() 
            {
                        public void onCancel(DialogInterface dialog) 
                        {
                            connector.cancel();
                        }
                    });
            new Thread(connector).start();
        } 
        catch (IOException ex) 
        {
            Log.d(TAG, "Could not open bluetooth socket", ex);
        }
    }

    private class ConnectRunnable implements Runnable 
    {
        private final ProgressDialog dialog;
        private final BluetoothSocket socket;

        public ConnectRunnable(BluetoothDevice device, ProgressDialog dialog) throws IOException 
        {
            socket = device.createRfcommSocketToServiceRecord(applicationUUID);
            this.dialog = dialog;
        }

        public void run() 
        {
            try 
            {
                Log.v(TAG, "InsideRunnableCalled");
                mBluetoothAdapter.cancelDiscovery();
                socket.connect();
                Log.v(TAG, "InsideRunnableSocketConnectCalled");
            }
            catch (IOException connectException) 
            {
                Log.d(TAG, "Could not connect to socket", connectException);
                closeSocket(socket);
                return;
            }
            Log.v(TAG, "Connected");
            dismissDialog(dialog);
            closeSocket(socket);
        }

        public void cancel() 
        {
            try 
            {
                socket.close();
            }
            catch (IOException e) 
            {
                Log.d(TAG, "Canceled connection", e);
            }
        }
    }

    private void dismissDialog(final Dialog dialog) 
    {
        runOnUiThread(new Runnable() 
        {
            public void run() 
            {
                dialog.dismiss();
                Log.v(TAG, "DialogClosed");
            }
        });
    }

    private void closeSocket(BluetoothSocket nOpenSocket) 
    {
        try 
        {
            nOpenSocket.close();
            Log.v(TAG, "SockectClosed");
        } 
        catch (IOException ex) 
        {
            Log.d(TAG, "Could not close exisiting socket", ex);
        }
    }
    @Override
    public boolean onKeyDown(int mKeyCode, KeyEvent mKeyEvent) 
    {
        if ((!(android.os.Build.VERSION.SDK_INT > android.os.Build.VERSION_CODES.DONUT)
            && mKeyCode == KeyEvent.KEYCODE_BACK && mKeyEvent.getRepeatCount() == 0)) 
        {
            onBackPressed();
        }
        return super.onKeyDown(mKeyCode, mKeyEvent);
    }

    public void onBackPressed() 
    {
        finish();
    }
}

DeviceListActivity.java DeviceListActivity.java

import java.util.Set;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class DeviceListActivity extends Activity 
{
    protected static final String TAG = "TAG";
    private BluetoothAdapter mBluetoothAdapter;
    private ArrayAdapter<String> mPairedDevicesArrayAdapter;

    @Override
    protected void onCreate(Bundle mSavedInstanceState) 
    {
        super.onCreate(mSavedInstanceState);
        requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
        setContentView(R.layout.device_list);

        setResult(Activity.RESULT_CANCELED);
        mPairedDevicesArrayAdapter = new ArrayAdapter<String>(this, R.layout.device_name);

        ListView mPairedListView = (ListView) findViewById(R.id.paired_devices);
        mPairedListView.setAdapter(mPairedDevicesArrayAdapter);
        mPairedListView.setOnItemClickListener(mDeviceClickListener);

        mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        Set<BluetoothDevice> mPairedDevices = mBluetoothAdapter.getBondedDevices();

        if (mPairedDevices.size() > 0) 
        {
            findViewById(R.id.title_paired_devices).setVisibility(View.VISIBLE);
            for (BluetoothDevice mDevice : mPairedDevices) 
            {
                mPairedDevicesArrayAdapter.add(mDevice.getName() + "\n" + mDevice.getAddress());
            }
        } 
        else 
        {
            String mNoDevices = getResources().getText(R.string.none_paired).toString();
            mPairedDevicesArrayAdapter.add(mNoDevices);
        }
    }

    @Override
    protected void onDestroy() 
    {
        super.onDestroy();
        if (mBluetoothAdapter != null) 
        {
            mBluetoothAdapter.cancelDiscovery();
        }
    }

    private OnItemClickListener mDeviceClickListener = new OnItemClickListener() 
    {
        public void onItemClick(AdapterView<?> mAdapterView, View mView, int mPosition, long mLong) 
        {
            mBluetoothAdapter.cancelDiscovery();
            String mDeviceInfo = ((TextView) mView).getText().toString();
            String mDeviceAddress = mDeviceInfo.substring(mDeviceInfo.length() - 17);
            Log.v(TAG, "Device_Address " + mDeviceAddress);

            Bundle mBundle = new Bundle();
            mBundle.putString("DeviceAddress", mDeviceAddress);
            Intent mBackIntent = new Intent();
            mBackIntent.putExtras(mBundle);
            setResult(Activity.RESULT_OK, mBackIntent);
            finish();
        }
    };

}

Android Manifest Android Manifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.androidvouge.bluetooth" android:versionCode="1"
    android:versionName="1.0">
        <application android:icon="@drawable/icon" android:label="@string/name"
        android:theme="@android:style/Theme.NoTitleBar.Fullscreen">
            <activity android:name=".Main" android:screenOrientation="portrait">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".DeviceListActivity"     android:theme="@android:style/Theme.Dialog"
        android:configChanges="orientation|keyboardHidden"
        android:screenOrientation="portrait" />
    </application>
    <uses-sdk android:minSdkVersion="7" android:targetSdkVersion="11" />
    <uses-permission android:name="android.permission.BLUETOOTH" />
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
    <supports-screens android:smallScreens="false"
        android:normalScreens="true" android:largeScreens="true" />
</manifest>

device_list.xml device_list.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <TextView android:id="@+id/title_paired_devices"
        android:layout_width="fill_parent" android:layout_height="wrap_content"
        android:text="@string/title_paired_devices" android:visibility="gone"
        android:background="#666" android:textColor="#fff"
        android:paddingLeft="5dip" />
    <ListView android:id="@+id/paired_devices"
        android:layout_width="fill_parent" android:layout_height="wrap_content"
        android:stackFromBottom="true" android:layout_weight="1" />
</LinearLayout>

device_name.xml device_name.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="wrap_content"
    android:textSize="18sp" android:padding="5dip" />

main.xml has only button. main.xml只有按钮。

Here is the logcat output. 这是logcat输出。

06-16 13:46:08.640 19062 19062 E CachedBluetoothDevice: updating profiles for $ $ S A \/ A N $ $
06-16 13:46:08.640 19062 19062 V CachedBluetoothDevice: opp classbits != uuid
06-16 13:46:08.640 19062 19062 V CachedBluetoothDevice: Class: 5a0204
06-16 13:46:08.640 19062 19062 V CachedBluetoothDevice: UUID:
06-16 13:46:08.640 19062 19062 V CachedBluetoothDevice:   00001101-0000-1000-8000-00805f9b34fb
06-16 13:46:08.667 19091 19091 V TAG     : PairedDevices: $ $ S A \/ A N $ $ 00:1D:3B:05:B4:D5
06-16 13:46:10.582 19091 19091 V TAG     : Device_Address 00:1D:3B:05:B4:D5
06-16 13:46:10.597 19091 19091 V TAG     : Coming incoming address 00:1D:3B:05:B4:D5
06-16 13:46:10.597 19091 19091 V TAG     : InsidepairToDeviceCalled
06-16 13:46:10.597 19091 19091 V TAG     : InsideOpenSockedCalled
06-16 13:46:10.718 19091 19091 V TAG     : InsideOpenSockedConnecterCalled
06-16 13:46:10.789 19091 19091 V TAG     : LeavingpairToDeviceCalled
06-16 13:46:10.789 19091 19091 V TAG     : pairToDeviceCalled
06-16 13:46:10.800 19091 19128 V TAG     : InsideRunnableCalled
06-16 13:46:13.730 19091 19128 V TAG     : InsideRunnableSocketConnectCalled
06-16 13:46:13.730 19091 19128 V TAG     : Connected
06-16 13:46:13.730 19091 19128 V TAG     : SockectClosed
06-16 13:46:13.734 19091 19091 V TAG     : DialogClosed

Where am going wrong? 哪里出错了? Anybody who has successfully configured Bluetooth with processing dialog, Please give me a hand. 任何已成功配置蓝牙处理对话框的人,请帮我一臂之力。

Thanks. 谢谢。

Please modify your Main.java file like below and please keep the rest files at it is. 请修改您的Main.java文件,如下所示,请保留其余文件。

Main.java Main.java

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Random;
import java.util.Set;
import java.util.TimeZone;
import java.util.UUID;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.PorterDuff.Mode;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.preference.PreferenceManager;
import android.provider.Settings;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class Main extends Activity implements Runnable 
{
    protected static final String TAG = "TAG";
    private static final int REQUEST_CONNECT_DEVICE = 1;
    private static final int REQUEST_ENABLE_BT = 2;
    Button mScan;
    BluetoothAdapter mBluetoothAdapter;
    private UUID applicationUUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
    private ProgressDialog mBluetoothConnectProgressDialog;
    private BluetoothSocket mBluetoothSocket;
    BluetoothDevice mBluetoothDevice;

    @Override
    public void onCreate(Bundle mSavedInstanceState) 
    {
        super.onCreate(mSavedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.main);
        mScan = (Button) findViewById(R.id.Scan);
        mScan.setOnClickListener(new View.OnClickListener() 
        {
            public void onClick(View mView) 
            {
                mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
                if (mBluetoothAdapter == null) 
                {
                    Toast.makeText(Main.this, "Message1", 2000).show();
                } 
                else 
                {
                    if (!mBluetoothAdapter.isEnabled()) 
                    {
                        Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
                        startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
                    } 
                    else 
                    {
                        ListPairedDevices();
                        Intent connectIntent = new Intent(Main.this, DeviceListActivity.class);
                        startActivityForResult(connectIntent, REQUEST_CONNECT_DEVICE);
                    }
                }
            }
        });

        mTune.setOnClickListener(new View.OnClickListener() 
        {
            public void onClick(View mView) 
            {
                mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
                if (mBluetoothAdapter == null) 
                {
                    Toast.makeText(Main.this, "Message2", 2000).show();
                } 
                else 
                {
                    if (!mBluetoothAdapter.isEnabled()) 
                    {
                        Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
                        startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
                    } 
                    else 
                    {
                        ListPairedDevices();
                        Intent connectIntent = new Intent(Main.this, DeviceListActivity.class);
                        startActivityForResult(connectIntent, REQUEST_CONNECT_DEVICE);
                    }
                }
            }
        });
    }// onCreate

    public void onActivityResult(int mRequestCode, int mResultCode, Intent mDataIntent) 
    {
        super.onActivityResult(mRequestCode, mResultCode, mDataIntent);

        switch (mRequestCode) 
        {
            case REQUEST_CONNECT_DEVICE:
                if (mResultCode == Activity.RESULT_OK) 
                {
                    Bundle mExtra = mDataIntent.getExtras();
                    String mDeviceAddress = mExtra.getString("DeviceAddress");
                    Log.v(TAG, "Coming incoming address " + mDeviceAddress);
                    mBluetoothDevice = mBluetoothAdapter.getRemoteDevice(mDeviceAddress);
                    mBluetoothConnectProgressDialog = ProgressDialog.show(this, "Connecting...", mBluetoothDevice.getName() + " : " + mBluetoothDevice.getAddress(), true, false);
                    Thread mBlutoothConnectThread = new Thread(this);
                    mBlutoothConnectThread.start();
                    //pairToDevice(mBluetoothDevice); This method is replaced by progress dialog with thread
                }
                break;

            case REQUEST_ENABLE_BT:
                if (mResultCode == Activity.RESULT_OK) 
                {
                    ListPairedDevices();
                    Intent connectIntent = new Intent(Main.this, DeviceListActivity.class);
                    startActivityForResult(connectIntent, REQUEST_CONNECT_DEVICE);
                } 
                else 
                {
                    Toast.makeText(Main.this, "Message", 2000).show();
                }
                break;
        }
    }

    private void ListPairedDevices() 
    {
        Set<BluetoothDevice> mPairedDevices = mBluetoothAdapter.getBondedDevices();
        if (mPairedDevices.size() > 0) 
        {
            for (BluetoothDevice mDevice : mPairedDevices) 
            {
                Log.v(TAG, "PairedDevices: " + mDevice.getName() + " " + mDevice.getAddress());
            }
        }
    }

    public void run() 
    {
        try 
        {
            mBluetoothSocket = mBluetoothDevice.createRfcommSocketToServiceRecord(applicationUUID);
            mBluetoothAdapter.cancelDiscovery();
            mBluetoothSocket.connect();
            mHandler.sendEmptyMessage(0);
        }
        catch (IOException eConnectException) 
        {
            Log.d(TAG, "CouldNotConnectToSocket", eConnectException);
             closeSocket(mBluetoothSocket);
             return;
        }
    }

    private void closeSocket(BluetoothSocket nOpenSocket) 
    {
        try 
        {
            nOpenSocket.close();
            Log.d(TAG, "SocketClosed");
        } 
        catch (IOException ex) 
        {
            Log.d(TAG, "CouldNotCloseSocket");
        }
    }

    private Handler mHandler = new Handler() 
    {
        @Override
        public void handleMessage(Message msg) 
        {
            mBluetoothConnectProgressDialog.dismiss();
            Toast.makeText(Main.this, "DeviceConnected", 5000).show();
        }
    };
}

Thanks. 谢谢。

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

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