蓝牙代码.docx

上传人:b****6 文档编号:7486703 上传时间:2023-05-11 格式:DOCX 页数:16 大小:19.02KB
下载 相关 举报
蓝牙代码.docx_第1页
第1页 / 共16页
蓝牙代码.docx_第2页
第2页 / 共16页
蓝牙代码.docx_第3页
第3页 / 共16页
蓝牙代码.docx_第4页
第4页 / 共16页
蓝牙代码.docx_第5页
第5页 / 共16页
蓝牙代码.docx_第6页
第6页 / 共16页
蓝牙代码.docx_第7页
第7页 / 共16页
蓝牙代码.docx_第8页
第8页 / 共16页
蓝牙代码.docx_第9页
第9页 / 共16页
蓝牙代码.docx_第10页
第10页 / 共16页
蓝牙代码.docx_第11页
第11页 / 共16页
蓝牙代码.docx_第12页
第12页 / 共16页
蓝牙代码.docx_第13页
第13页 / 共16页
蓝牙代码.docx_第14页
第14页 / 共16页
蓝牙代码.docx_第15页
第15页 / 共16页
蓝牙代码.docx_第16页
第16页 / 共16页
亲,该文档总共16页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

蓝牙代码.docx

《蓝牙代码.docx》由会员分享,可在线阅读,更多相关《蓝牙代码.docx(16页珍藏版)》请在冰点文库上搜索。

蓝牙代码.docx

蓝牙代码

importcom.util.SlipButton;

importcom.util.SlipButton.OnChangedListener;

publicclassActivity01extendsActivity{

/*取得默认的蓝牙适配器*/

privateBluetoothAdapter_bluetooth=BluetoothAdapter.getDefaultAdapter();

privateHandler_handler=newHandler();

ArrayAdapteradapter=null;

/*请求打开蓝牙*/

@SuppressWarnings("unused")

privatestaticfinalintREQUEST_ENABLE=0x1;

privatestaticfinalintREQUEST_EX=1;

/*请求能够被搜索*/

privatestaticfinalintREQUEST_DISCOVERABLE=0x2;

privateList_devices=newArrayList();

privateList_dev=newArrayList();

privateSetdev=null;

privateBluetoothSocketsocket;//蓝牙连接socket

privateHandlermOthHandler;//其它线程Handler

privateBluetoothDevicefiledBluetoothDevice;

/*是否完成搜索*/

privatevolatileboolean_discoveryFinished;

ListViewlistView=null;

SlipButtonbutton=null;

SlipButtonsplitbutton3=null;

SlipButtonsplitbutton4=null;

layoutlayname=null;

Buttonringagain2=null;

/**Calledwhentheactivityisfirstcreated.*/

@Override

publicvoidonCreate(BundlesavedInstanceState){

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

/*

***初始化控件**

*****

*/

findView();

/*

***控件设置事件**

****

*/

setListener();

}

/**

*初始化控件

*/

privatevoidfindView(){

button=(SlipButton)findViewById(R.id.splitbutton1);

splitbutton3=(SlipButton)findViewById(R.id.splitbutton3);

splitbutton4=(SlipButton)findViewById(R.id.splitbutton4);

ringagain2=(Button)findViewById(R.id.ringagain2);

listView=(ListView)findViewById(R.id.lvListid);

//蓝牙没有被打开时,可发现按钮不可用

/*

***客户端**\l

*******

*/

splitbutton3.setEnabled(false);

splitbutton4.setEnabled(false);

Log.e("ringagain2","_bluetooth");

ringagain2.setText(_bluetooth.getName());

}

/*

*事件监听器

*/

privatevoidsetListener(){

button.SetOnChangedListener(newOnChangedListener(){

@Override

publicvoidOnChanged(booleanCheckState){

//TODOAuto-generatedmethodstub

//滑动按钮打开时的动作

if(CheckState){

//打开蓝牙

_bluetooth.enable();

//按钮使能

splitbutton3.setEnabled(true);

splitbutton4.setEnabled(true);

}else{

//滑动按钮关闭时的动作

_bluetooth.disable();

splitbutton3.setEnabled(false);

splitbutton4.setEnabled(false);

}

}

});

splitbutton3.SetOnChangedListener(newOnChangedListener(){

@Override

publicvoidOnChanged(booleanCheckState){

//TODOAuto-generatedmethodstub

if(CheckState){

Intentenabler=newIntent(

BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);

startActivityForResult(enabler,REQUEST_DISCOVERABLE);

}

}

});

splitbutton4.SetOnChangedListener(newOnChangedListener(){

@Override

publicvoidOnChanged(booleanCheckState){

//TODOAuto-generatedmethodstub

/*注册接收器*/

dev=_bluetooth.getBondedDevices();

_dev.clear();

for(BluetoothDevicedevice:

dev){

_dev.add(device);

}

if(CheckState){

_discoveryFinished=false;

_devices.retainAll(_devices);

_devices=_dev;

IntentFilterdiscoveryFilter=newIntentFilter(

BluetoothAdapter.ACTION_DISCOVERY_FINISHED);

registerReceiver(_discoveryReceiver,discoveryFilter);

IntentFilterfoundFilter=newIntentFilter(

BluetoothDevice.ACTION_FOUND);

registerReceiver(_foundReceiver,foundFilter);

/*显示一个对话框,正在搜索蓝牙设备*/

SamplesUtils.indeterminate(Activity01.this,_handler,

"Scanning...",_discoveryWorkder,

newOnDismissListener(){

publicvoidonDismiss(DialogInterfacedialog){

for(;_bluetooth.isDiscovering();){

_bluetooth.cancelDiscovery();

}

_discoveryFinished=true;

}

},true);

}else{

_discoveryFinished=true;

_devices.retainAll(_devices);

_devices=_dev;

showDevices();

}

}

});

ringagain2.setOnClickListener(newOnClickListener(){

@Override

publicvoidonClick(Viewv){

//TODOAuto-generatedmethodstub

//new

//AlertDialog.Builder(Activity01.this).setView(layname).show();

AlertDialog.Builderbuilder=newAlertDialog.Builder(

Activity01.this);

finalEditTexteditText=newEditText(Activity01.this);

builder.setTitle("请输入")

.setIcon(android.R.drawable.ic_dialog_info)

.setView(editText)

.setPositiveButton("确定",

newDialogInterface.OnClickListener(){

@Override

publicvoidonClick(DialogInterfacedialog,

intwhich){

//TODOAuto-generatedmethodstub

StringdevName=editText.getText()

.toString();

ringagain2.setText(devName);

_bluetooth.setName(devName);

}

}).setNegativeButton("取消",null).show();

}

});

listView.setOnItemClickListener(itemClickListener);

}

/**

*接收器当搜索蓝牙设备完成时调用

*/

privateBroadcastReceiver_foundReceiver=newBroadcastReceiver(){

publicvoidonReceive(Contextcontext,Intentintent){

/*从intent中取得搜索结果数据*/

BluetoothDevicedevice=intent

.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);

/*将结果添加到列表中*/

if(device.getBondState()==BluetoothDevice.BOND_NONE)

{

_devices.add(device);

}

/*显示列表*/

showDevices();

}

};

privateBroadcastReceiver_discoveryReceiver=newBroadcastReceiver(){

@Override

publicvoidonReceive(Contextcontext,Intentintent){

/*卸载注册的接收器*/

unregisterReceiver(_foundReceiver);

unregisterReceiver(this);

_discoveryFinished=true;

}

};

/*显示状态列表*/

protectedvoidshowDevices(){

Listlist=newArrayList();

for(inti=0,size=_devices.size();i

StringBuilderb=newStringBuilder();

BluetoothDeviced=_devices.get(i);

b.append(d.getName());

b.append('\n');

if(d.getBondState()==BluetoothDevice.BOND_NONE)

{

b.append("未配对");

}elseif(d.getBondState()==BluetoothDevice.BOND_BONDED){

b.append("已配对");

}elseif(d.getBondState()==BluetoothDevice.BOND_BONDING){

b.append("正在配对");

}

Strings=b.toString();

list.add(s);

}

adapter=newArrayAdapter(this,

android.R.layout.simple_list_item_1,list);

_handler.post(newRunnable(){

publicvoidrun(){

listView.setAdapter(adapter);

}

});

}

privateRunnable_discoveryWorkder=newRunnable(){

publicvoidrun(){

/*开始搜索*/

_bluetooth.startDiscovery();

for(;;){

if(_discoveryFinished){

break;

}

try{

Thread.sleep(100);

}catch(InterruptedExceptione){

}

}

}

};

privateListView.OnItemClickListeneritemClickListener=newListView.OnItemClickListener(){

@Override

publicvoidonItemClick(AdapterView

>arg0,Viewarg1,intarg2,

longarg3){

//Toast.makeText(Activity01.this,"adfasfsadf",Toast.LENGTH_SHORT).show();

finalintindex=arg2;

finalBluetoothDevicedev=_devices.get(index);

filedBluetoothDevice=dev;

if(dev.getBondState()==BluetoothDevice.BOND_NONE){

AlertDialog.Builderdialog=newAlertDialog.Builder(Activity01.this);

dialog.setTitle("是否配对")

.setIcon(android.R.drawable.ic_dialog_info)

.setPositiveButton("配对",newDialogInterface.OnClickListener(){

@Override

publicvoidonClick(DialogInterfacedialog,intwhich){

//TODOAuto-generatedmethodstub

try{

MethodcreateBondMethod=BluetoothDevice.class.getMethod("createBond");

Toast.makeText(Activity01.this,"开始配对",Toast.LENGTH_SHORT).show();

createBondMethod.invoke(dev);

BluetoothDevicedv=null;

dv=_bluetooth.getRemoteDevice(dev.getAddress());

_dev.set(index,dv);

showDevices();

}catch(SecurityExceptione){

//TODOAuto-generatedcatchblock

e.printStackTrace();

}catch(NoSuchMethodExceptione){

//TODOAuto-generatedcatchblock

e.printStackTrace();

}catch(IllegalArgumentExceptione){

//TODOAuto-generatedcatchblock

e.printStackTrace();

}catch(IllegalAccessExceptione){

//TODOAuto-generatedcatchblock

e.printStackTrace();

}catch(InvocationTargetExceptione){

//TODOAuto-generatedcatchblock

e.printStackTrace();

}

}

})

.setNegativeButton("关闭",null)

//.setNegativeButton("配对完成",null)

.show();

}else{

AlertDialog.Builderdialog=newAlertDialog.Builder(Activity01.this);

dialog.setTitle("取消配对或连接")

.setIcon(android.R.drawable.ic_dialog_info)

.setPositiveButton("取消配对",newDialogInterface.OnClickListener(){

@Override

publicvoidonClick(DialogInterfacedialog,intwhich){

//TODOAuto-generatedmethodstub

try{

MethodremoveBondMethod=BluetoothDevice.class.getMethod("removeBond");

Toast.makeText(Activity01.this,"取消配对",Toast.LENGTH_SHORT).show();

removeBondMethod.invoke(dev);

BluetoothDevicedv=null;

dv=_bluetooth.getRemoteDevice(dev.getAddress());

_dev.set(index,dv);

showDevices();

}catch(SecurityExceptione){

//TODOAuto-generatedcatchblock

e.printStackTrace();

}catch(NoSuchMethodExceptione){

//TODOAuto-generatedcatchblock

e.printStackTrace();

}catch(IllegalArgumentExceptione){

//TODOAuto-generatedcatchblock

e.printStackTrace();

}catch(IllegalAccessExceptione){

//TODOAuto-generatedcatchblock

e.printStackTrace();

}catch(InvocationTarg

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > PPT模板 > 动物植物

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2