码迷,mamicode.com
首页 > 移动开发 > 详细

Android开发之蓝牙连接打印机

时间:2017-09-24 15:29:04      阅读:298      评论:0      收藏:0      [点我收藏+]

标签:weixin   else   param   isa   arch   nbu   ==   布局文件   request   

代码很简单,直接一个布局文件和一个activity。需要的朋友可以直接将这两部分粘贴复制到项目中即可。

技术分享

Activity部分:

package com.anhua.bluetooth;

import java.io.IOException;
import java.io.OutputStream;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.UUID;
import com.example.imooc_weixinfragment.R;
import android.app.Activity;
import android.app.ProgressDialog;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class BluetoothActivity extends Activity implements OnClickListener {

    private ListView unbondDevices = null;
    private ListView bondDevices = null;
    private Context context = null;
    private BluetoothAdapter bluetoothAdapter = BluetoothAdapter
            .getDefaultAdapter();
    private ArrayList<BluetoothDevice> unbondDeviceslist = null; // 用于存放未配对蓝牙设备
    private ArrayList<BluetoothDevice> bondDeviceslist = null;// 用于存放已配对蓝牙设备
    private ListView unbondDevicesListView = null;
    private ListView bondDevicesListView = null;
    private Button searchDevices;// 搜索蓝牙设备Button
    private TextView deviceName;// 蓝牙设备的名字
    private TextView connectState = null;//蓝牙设备连接状态
    private String address;// 蓝牙地址
    private ImageView returnButton;
    private boolean isConnection = false;
    private BluetoothDevice devices = null;
    private static BluetoothSocket bluetoothSocket = null;
    private static final UUID uuid = UUID
            .fromString("00001101-0000-1000-8000-00805F9B34FB");
    private static OutputStream outputStream = null;
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.context = this;
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.bluetooth_layout);
        unbondDeviceslist = new ArrayList<BluetoothDevice>();
        bondDeviceslist = new ArrayList<BluetoothDevice>();
        this.initIntentFilter();
        initView();
        init();
    }

    private void init() {
        deviceName=(TextView) findViewById(R.id.device_name);
        connectState=(TextView) findViewById(R.id.connect_state);
        unbondDevices = (ListView) this.findViewById(R.id.unbondDevices);
        bondDevices = (ListView) this.findViewById(R.id.bondDevices);
        searchDevices = (Button) this.findViewById(R.id.searchDevices);
        unbondDevicesListView = (ListView) findViewById(R.id.unbondDevices);
        bondDevicesListView = (ListView) findViewById(R.id.bondDevices);
        returnButton = (ImageView) this.findViewById(R.id.return_Bluetooth_btn);
        this.setSearchDevices(searchDevices);
        searchDevices.setOnClickListener(this);
        returnButton.setOnClickListener(this);
    }

    public void setSearchDevices(Button searchDevices) {
        this.searchDevices = searchDevices;
    }

    /**
     * 初始化界面
     */
    public void initView() {

        if (this.isOpen()) {
            System.out.println("蓝牙有开!");
        }
        if (!this.isOpen()) {
            System.out.println("蓝牙没开!");
            this.searchDevices.setEnabled(false);
        }
    }

    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        this.disconnect();
        super.onDestroy();
    }

    /**
     * 添加已绑定蓝牙设备到ListView
     */
    private void addBondDevicesToListView() {
        ArrayList<HashMap<String, Object>> data = new ArrayList<HashMap<String, Object>>();
        int count = this.bondDeviceslist.size();
        System.out.println("已绑定设备数量:" + count);
        for (int i = 0; i < count; i++) {
            HashMap<String, Object> map = new HashMap<String, Object>();
            map.put("deviceName", this.bondDeviceslist.get(i).getName());
            data.add(map);// 把item项的数据加到data中
        }
        String[] from = { "deviceName" };
        int[] to = { R.id.device_name };
        SimpleAdapter simpleAdapter = new SimpleAdapter(this.context, data,
                R.layout.bonddevice_item, from, to);
        // 把适配器装载到listView中
        this.bondDevicesListView.setAdapter(simpleAdapter);

        this.bondDevicesListView
                .setOnItemClickListener(new OnItemClickListener() {

                    @Override
                    public void onItemClick(AdapterView<?> arg0, View arg1,
                            int arg2, long arg3) {
                        deviceName.setVisibility(View.VISIBLE);
                        connectState.setVisibility(View.VISIBLE);
                        BluetoothDevice device = bondDeviceslist.get(arg2);
                        address = device.getAddress();// 获取蓝牙地址
                        devices=bluetoothAdapter.getRemoteDevice(address);
                        
                        deviceName.setText(getDeviceName());
                        // 一上来就先连接蓝牙设备
                        boolean flag = connect();
                        if (flag == false) {
                            // 连接失败
                            connectState.setText("连接失败!");
                        } else {
                            // 连接成功
                            connectState.setText("连接成功!");
                        }
                    }
                });
    }

    /**
     * 添加未绑定蓝牙设备到ListView
     */
    private void addUnbondDevicesToListView() {
        ArrayList<HashMap<String, Object>> data = new ArrayList<HashMap<String, Object>>();
        int count = this.unbondDeviceslist.size();
        System.out.println("未绑定设备数量:" + count);
        for (int i = 0; i < count; i++) {
            HashMap<String, Object> map = new HashMap<String, Object>();
            map.put("deviceName", this.unbondDeviceslist.get(i).getName());
            data.add(map);// 把item项的数据加到data中
        }
        String[] from = { "deviceName" };
        int[] to = { R.id.undevice_name };
        SimpleAdapter simpleAdapter = new SimpleAdapter(this.context, data,
                R.layout.unbonddevice_item, from, to);

        // 把适配器装载到listView中
        unbondDevicesListView.setAdapter(simpleAdapter);

        // 为每个item绑定监听,用于设备间的配对
        unbondDevicesListView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                    long arg3) {
                try {
                    Method createBondMethod = BluetoothDevice.class
                            .getMethod("createBond");
                    createBondMethod.invoke(unbondDeviceslist.get(arg2));
                    // 将绑定好的设备添加的已绑定list集合
                    bondDeviceslist.add(unbondDeviceslist.get(arg2));
                    // 将绑定好的设备从未绑定list集合中移除
                    unbondDeviceslist.remove(arg2);
                    addBondDevicesToListView();
                    addUnbondDevicesToListView();
                } catch (Exception e) {
                    Toast.makeText(context, "配对失败!", Toast.LENGTH_SHORT).show();
                }

            }
        });
    }

    private void initIntentFilter() {
        // 设置广播信息过滤
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(BluetoothDevice.ACTION_FOUND);
        intentFilter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
        intentFilter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
        intentFilter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
        // 注册广播接收器,接收并处理搜索结果
        context.registerReceiver(receiver, intentFilter);

    }

    /**
     * 打开蓝牙
     */
    public void openBluetooth(Activity activity) {
        Intent enableBtIntent = new Intent(
                BluetoothAdapter.ACTION_REQUEST_ENABLE);
        activity.startActivityForResult(enableBtIntent, 1);

    }

    /**
     * 关闭蓝牙
     */
    public void closeBluetooth() {
        this.bluetoothAdapter.disable();
    }

    /**
     * 判断蓝牙是否打开
     * 
     * @return boolean
     */
    public boolean isOpen() {
        return this.bluetoothAdapter.isEnabled();

    }

    /**
     * 搜索蓝牙设备
     */
    public void searchDevices() {
        this.bondDeviceslist.clear();
        this.unbondDeviceslist.clear();

        // 寻找蓝牙设备,android会将查找到的设备以广播形式发出去
        this.bluetoothAdapter.startDiscovery();
    }

    /**
     * 添加未绑定蓝牙设备到list集合
     * 
     * @param device
     */
    public void addUnbondDevices(BluetoothDevice device) {
        System.out.println("未绑定设备名称:" + device.getName());
        if (!this.unbondDeviceslist.contains(device)) {
            this.unbondDeviceslist.add(device);
        }
    }

    /**
     * 添加已绑定蓝牙设备到list集合
     * 
     * @param device
     */
    public void addBandDevices(BluetoothDevice device) {
        System.out.println("已绑定设备名称:" + device.getName());
        if (!this.bondDeviceslist.contains(device)) {
            this.bondDeviceslist.add(device);
        }
    }

    /**
     * 蓝牙广播接收器
     */
    private BroadcastReceiver receiver = new BroadcastReceiver() {

        ProgressDialog progressDialog = null;

        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (BluetoothDevice.ACTION_FOUND.equals(action)) {
                BluetoothDevice device = intent
                        .getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                if (device.getBondState() == BluetoothDevice.BOND_BONDED) {
                    addBandDevices(device);
                } else {
                    addUnbondDevices(device);
                }
            } else if (BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action)) {
                progressDialog = ProgressDialog.show(context, "请稍等...",
                        "搜索蓝牙设备中...", true);

            } else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED
                    .equals(action)) {
                System.out.println("设备搜索完毕");
                progressDialog.dismiss();

                addUnbondDevicesToListView();
                addBondDevicesToListView();
            }
        }
    };
    /**
     * 获取设备名称
     * 
     * @return String
     */
    public String getDeviceName() {
        return devices.getName();
    }
    /**
     * 连接蓝牙设备
     */
    public boolean connect() {
        if (!this.isConnection) {
            try {
                bluetoothSocket = this.devices
                        .createRfcommSocketToServiceRecord(uuid);
                bluetoothSocket.connect();
                outputStream = bluetoothSocket.getOutputStream();
                this.isConnection = true;
                if (this.bluetoothAdapter.isDiscovering()) {
                    System.out.println("关闭适配器!");
                    this.bluetoothAdapter.isDiscovering();
                }
            } catch (Exception e) {
                Toast.makeText(this.context, "连接失败!", 1).show();
                return false;
            }
            Toast.makeText(this.context, this.devices.getName() + "连接成功!",
                    Toast.LENGTH_SHORT).show();
            return true;
        } else {
            return true;
        }
    }
    /**
     * 断开蓝牙设备连接
     */
    public static void disconnect() {
        System.out.println("断开蓝牙设备连接");
        try {
            bluetoothSocket.close();
            outputStream.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.searchDevices:
            this.searchDevices();
            break;
        case R.id.return_Bluetooth_btn:
            finish();
            break;
        default:
            break;
        }
    }
}

布局文件部分:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/index"
    tools:context=".BluetoothActivity" >

    <LinearLayout
        android:id="@+id/openBluetooth_tb"
        android:layout_width="match_parent"
        android:layout_height="44dp"
        android:background="#24cf5f"
        android:orientation="horizontal" >

        <ImageView
            android:id="@+id/return_Bluetooth_btn"
            android:layout_width="match_parent"
            android:layout_height="20dp"
            android:layout_gravity="center"
            android:layout_weight="5"
            android:src="@drawable/back"/>

        <TextView
            android:layout_width="fill_parent"
            android:layout_height="44dp"
            android:layout_weight="1"
            android:gravity="center"
            android:paddingRight="40dp"
            android:text="@string/titie_activity_conn_blue_list"
            android:textColor="#FFFFFF"
            android:textSize="30px" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/openBluetooth_tb"
        android:orientation="vertical" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >

            <TextView
                android:visibility="gone"
                android:id="@+id/device_name"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="名称"
                android:textAppearance="?android:attr/textAppearanceLarge" />

            <TextView
                android:visibility="gone"
                android:id="@+id/connect_state"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="42dp"
                android:text="状态"
                android:textAppearance="?android:attr/textAppearanceLarge" />
        </LinearLayout>

        <Button
            android:id="@+id/searchDevices"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="搜索设备" />
         <View
        android:layout_width="match_parent"
        android:layout_height="3dp"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/searchDevices"
        android:background="@android:color/darker_gray" />
        
    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="match_parent"
        android:layout_height="150dp"
        android:orientation="vertical" >

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="未配对设备" />

        <ListView
            android:id="@+id/unbondDevices"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>
          <View
        android:layout_width="match_parent"
        android:layout_height="3dp"
        android:layout_alignParentLeft="true"
        android:background="@android:color/darker_gray" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="190dp"
        android:orientation="vertical" >

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="已配对设备" />

        <ListView
            android:id="@+id/bondDevices"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/linearLayout1" >
        </ListView>
    </LinearLayout>
    </LinearLayout>

</RelativeLayout>

通过以上两部分就可以实现连接蓝牙打印机的功能。

Android开发之蓝牙连接打印机

标签:weixin   else   param   isa   arch   nbu   ==   布局文件   request   

原文地址:http://www.cnblogs.com/1925yiyi/p/7587152.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!