基本信息
源码名称:android 蓝牙聊天通信Demo源码(发消息、开启、关闭蓝牙)
源码大小:1.93M
文件格式:.rar
开发语言:Java
更新时间:2016-01-21
友情提示:(无需注册或充值,赞助后即可获取资源下载链接)
嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300
本次赞助数额为: 2 元×
微信扫码支付:2 元
×
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
源码介绍
这是一个蓝牙连接通信的demo
package com.example.bluetoothconn; import java.util.ArrayList; import java.util.Set; import android.app.Activity; import android.app.AlertDialog; import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothDevice; import android.content.BroadcastReceiver; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.content.IntentFilter; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.Button; import android.widget.ListView; import android.widget.Toast; public class MainActivity extends Activity { static enum ServerOrCilent { NONE, SERVICE, CLIENT }; static String BlueToothAddress = "null"; static ServerOrCilent serviceOrCilent = ServerOrCilent.NONE; static boolean isOpen = false; private ListView mListView; private ArrayList<MyListItem> list; private Button seachButton, serviceButton, open, close; ListAdapter mAdapter; Context mContext; // 取得默认的蓝牙适配器 private BluetoothAdapter mBtAdapter = BluetoothAdapter.getDefaultAdapter(); boolean isBTOpen; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); getActionBar().setTitle("扫描到的设备"); setContentView(R.layout.activity_main); mContext = this; init(); } private void init() { list = new ArrayList<MyListItem>(); mAdapter = new ListAdapter(this, list); mListView = (ListView) findViewById(android.R.id.list); mListView.setAdapter(mAdapter); mListView.setFastScrollEnabled(true); mListView.setOnItemClickListener(mDeviceClickListener); // Register for broadcasts when a device is discovered IntentFilter discoveryFilter = new IntentFilter( BluetoothDevice.ACTION_FOUND); this.registerReceiver(mReceiver, discoveryFilter); // Get a set of currently paired devices Set<BluetoothDevice> pairedDevices = mBtAdapter.getBondedDevices(); // If there are paired devices, add each one to the ArrayAdapter if (pairedDevices.size() > 0) { for (BluetoothDevice device : pairedDevices) { list.add(new MyListItem(device.getName() "\n" device.getAddress(), true)); mAdapter.notifyDataSetChanged(); mListView.setSelection(list.size() - 1); } } else { list.add(new MyListItem("没有设备已经配对", true)); mAdapter.notifyDataSetChanged(); mListView.setSelection(list.size() - 1); } open = (Button) findViewById(R.id.open); open.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub isBTOpen = mBtAdapter.isEnabled(); if (!isBTOpen) { boolean open = mBtAdapter.enable(); if (open) { Toast.makeText(MainActivity.this, "蓝牙已打开", 1000).show(); } else { Toast.makeText(MainActivity.this, "打开蓝牙失败", 1000) .show(); } } else { Toast.makeText(MainActivity.this, "蓝牙已经处于开启状态,不需要再打开了", 1000).show(); } } }); close = (Button) findViewById(R.id.close); close.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub isBTOpen = mBtAdapter.isEnabled(); if (isBTOpen) { boolean close = mBtAdapter.disable(); if (close) { Toast.makeText(MainActivity.this, "蓝牙已关闭", 1000).show(); } else { Toast.makeText(MainActivity.this, "关闭蓝牙失败", 1000) .show(); } } else { Toast.makeText(MainActivity.this, "蓝牙已经处于关闭状态,不需要再关闭了", 1000).show(); } } }); serviceButton = (Button) findViewById(R.id.startService); serviceButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub isBTOpen = mBtAdapter.isEnabled(); if (isBTOpen) { serviceOrCilent = ServerOrCilent.SERVICE; Intent intent = new Intent(); intent.setClass(MainActivity.this, ChatActivity.class); startActivity(intent); } else { Toast.makeText(MainActivity.this, "蓝牙未开启,无法开启服务,请先开启蓝牙再开启服务。", 1000).show(); } } }); seachButton = (Button) findViewById(R.id.startSearch); seachButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub isBTOpen = mBtAdapter.isEnabled(); if (isBTOpen) { if (mBtAdapter.isDiscovering()) { mBtAdapter.cancelDiscovery(); seachButton.setText("重新搜索"); } else { list.clear(); mAdapter.notifyDataSetChanged(); Set<BluetoothDevice> pairedDevices = mBtAdapter .getBondedDevices(); if (pairedDevices.size() > 0) { for (BluetoothDevice device : pairedDevices) { list.add(new MyListItem(device.getName() "\n" device.getAddress(), true)); mAdapter.notifyDataSetChanged(); mListView.setSelection(list.size() - 1); } } else { list.add(new MyListItem( "No devices have been paired", true)); mAdapter.notifyDataSetChanged(); mListView.setSelection(list.size() - 1); } // 开始搜索 mBtAdapter.startDiscovery(); seachButton.setText("停止搜索"); } } else { Toast.makeText(MainActivity.this, "蓝牙处于关闭状态,请开启蓝牙再进行搜索", 1000).show(); } } }); } private OnItemClickListener mDeviceClickListener = new OnItemClickListener() { public void onItemClick(AdapterView<?> av, View v, int arg2, long arg3) { MyListItem item = list.get(arg2); String info = item.message; String address = info.substring(info.length() - 17); BlueToothAddress = address; AlertDialog.Builder StopDialog = new AlertDialog.Builder(mContext); StopDialog.setTitle("连接");// 标题 StopDialog.setMessage(item.message); StopDialog.setPositiveButton("连接", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub isBTOpen = mBtAdapter.isEnabled(); if (isBTOpen) { mBtAdapter.cancelDiscovery(); seachButton.setText("重新搜索"); serviceOrCilent = ServerOrCilent.CLIENT; Intent intent = new Intent(); intent.setClass(MainActivity.this, ChatActivity.class); startActivity(intent); } else { Toast.makeText(MainActivity.this, "蓝牙未打开,无法连接至服务端,请先打开蓝牙再进行连接", 1000) .show(); } } }); StopDialog.setNegativeButton("取消", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { BlueToothAddress = null; } }); StopDialog.show(); } }; private final BroadcastReceiver mReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); // When discovery finds a device if (BluetoothDevice.ACTION_FOUND.equals(action)) { // Get the BluetoothDevice object from the Intent BluetoothDevice device = intent .getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); // If it's already paired, skip it, because it's been listed // already if (device.getBondState() != BluetoothDevice.BOND_BONDED) { list.add(new MyListItem(device.getName() "\n" device.getAddress(), false)); mAdapter.notifyDataSetChanged(); mListView.setSelection(list.size() - 1); } // When discovery is finished, change the Activity title } else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED .equals(action)) { setProgressBarIndeterminateVisibility(false); if (mListView.getCount() == 0) { list.add(new MyListItem("没有发现蓝牙设备", false)); mAdapter.notifyDataSetChanged(); mListView.setSelection(list.size() - 1); } seachButton.setText("重新搜索"); } } }; @Override protected void onDestroy() { super.onDestroy(); // Make sure we're not doing discovery anymore if (mBtAdapter != null) { mBtAdapter.cancelDiscovery(); } // Unregister broadcast listeners } }