预览加载中,请您耐心等待几秒...
1/10
2/10
3/10
4/10
5/10
6/10
7/10
8/10
9/10
10/10

亲,该文档总共50页,到这已经超出免费预览范围,如果喜欢就直接下载吧~

如果您无法下载资料,请参考说明:

1、部分资料下载需要金币,请确保您的账户上有足够的金币

2、已购买过的文档,再次下载不重复扣费

3、资料包下载后请先用软件解压,在使用对应软件打开

packagecom.lxx; importjava.io.IOException; importjava.io.InputStream; importjava.io.OutputStream; importjava.util.UUID; importandroid.app.Service; importandroid.bluetooth.BluetoothAdapter; importandroid.bluetooth.BluetoothDevice; importandroid.bluetooth.BluetoothSocket; importandroid.content.BroadcastReceiver; importandroid.content.Context; importandroid.content.Intent; importandroid.content.IntentFilter; importandroid.os.IBinder; importandroid.util.Log; publicclassMyServiceextendsService{ publicbooleanthreadFlag=true; MyThreadmyThread; CommandReceivercmdReceiver;//继承自BroadcastReceiver对象,用于得到Activity发送过来的命令 /**************service命令*********/ staticfinalintCMD_STOP_SERVICE=0x01; staticfinalintCMD_SEND_DATA=0x02; staticfinalintCMD_SYSTEM_EXIT=0x03; staticfinalintCMD_SHOW_TOAST=0x04; privateBluetoothAdaptermBluetoothAdapter=null; privateBluetoothSocketbtSocket=null; privateOutputStreamoutStream=null; privateInputStreaminStream=null; publicbooleanbluetoothFlag=true; privatestaticfinalUUIDMY_UUID=UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"); privatestaticStringaddress="00:19:5D:EE:9B:8F";//<==要连接的蓝牙设备MAC地址 @Override publicIBinderonBind(Intentintent){ //TODOAuto-generatedmethodstub returnnull; } @Override publicvoidonCreate(){ //TODOAuto-generatedmethodstub super.onCreate(); } //前台Activity调用startService时,该方法自动执行 @Override publicintonStartCommand(Intentintent,intflags,intstartId){ //TODOAuto-generatedmethodstub cmdReceiver=newCommandReceiver(); IntentFilterfilter=newIntentFilter();//创建IntentFilter对象 //注册一个广播,用于接收Activity传送过来的命令,控制Service的行为,如:发送数据,停止服务等 filter.addAction("android.intent.action.cmd"); //注册BroadcastReceiver registerReceiver(cmdReceiver,filter); doJob();//调用方法启动线程 returnsuper.onStartCommand(intent,flags,startId); } @Override publicvoidonDestroy(){ //TODOAuto-generatedmethodstub super.onDestroy(); this.unregisterReceiver(cmdReceiver);//取消注册的CommandReceiver threadFlag=false; booleanretry=true; while(retry){ try{ myThread.join(); retry=false