热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

蓝牙传输数据java代码程序,android蓝牙数据传输例子

【实例简介】android4.0开发【实例截图】【核心代码】packagecn.novacomm.igatelibtest;importjava.io.UnsupportedEnc

【实例简介】android4.0开发

【实例截图】

【核心代码】

package cn.novacomm.igatelibtest;

import java.io.UnsupportedEncodingException;

import java.util.ArrayList;

import java.util.UUID;

import android.app.Activity;

import android.bluetooth.BluetoothAdapter;

import android.bluetooth.BluetoothDevice;

import android.content.BroadcastReceiver;

import android.content.Context;

import android.content.Intent;

import android.content.IntentFilter;

import android.os.Bundle;

import android.text.method.ScrollingMovementMethod;

import android.util.Log;

import android.view.Menu;

import android.view.MenuItem;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.TextView;

import android.widget.Button;

import cn.novacomm.ble.iGate;

import cn.novacomm.ble.iGateCallBacks;

public class MainActivity extends Activity implements iGateCallBacks{

private static final int ENABLE_BT_REQUEST_ID = 1;

private TextView mIGateState;

private TextView mReceivedData;

private TextView mSendMsg;

private Button mButtonSend;

private ArrayList mConnectedBluetoothDevicesAddress=new ArrayList();

private int sendIndex=0;

private BroadcastReceiver mPairIntentReceiver=new BroadcastReceiver(){

public void onReceive(Context context, Intent intent) {

if (BluetoothDevice.ACTION_BOND_STATE_CHANGED.equals(intent.getAction()))

{

int bondState = intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE, -1);

BluetoothDevice aDevice=intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);

if(bondState==BluetoothDevice.BOND_BONDED){

mIgate.iGateDeviceSetPairState(aDevice.getAddress(), true);

}

}

}

};

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

mIGateState=(TextView) findViewById(R.id.textViewState);

mReceivedData=(TextView) findViewById(R.id.textViewRxData);

mSendMsg=(TextView) findViewById(R.id.editTextSendStr);

mSendMsg.setText("&N 29604");

mReceivedData.setMovementMethod(ScrollingMovementMethod.getInstance());

mButtonSend = (Button) findViewById(R.id.buttonSend);

mButtonSend.setOnClickListener(new OnClickListener() {

public void onClick(View v) {

TextView view = (TextView) findViewById(R.id.editTextSendStr);

String message = view.getText().toString();

if(sendIndex

mIgate.iGateDeviceSendData(mConnectedBluetoothDevicesAddress.get(sendIndex), message.getBytes());

}

else{

sendIndex=0;

if(mConnectedBluetoothDevicesAddress.size()>0){

mIgate.iGateDeviceSendData(mConnectedBluetoothDevicesAddress.get(sendIndex), message.getBytes());

}

}

sendIndex ;

if(sendIndex==mConnectedBluetoothDevicesAddress.size()){

sendIndex=0;

}

}

});

mIgate = new iGate(this, UUID.fromString("C14D2C0A-401F-B7A9-841F-E2E93B80F631") ,this);

}

@Override

protected void onResume() {

super.onResume();

mIgate.initialize(false);

IntentFilter bluetoothPairFilter = new IntentFilter();

bluetoothPairFilter.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED);

registerReceiver(mPairIntentReceiver, bluetoothPairFilter);

}

@Override

protected void onPause() {

super.onPause();

unregisterReceiver(mPairIntentReceiver);

}

@Override

public boolean onCreateOptionsMenu(Menu menu) {

getMenuInflater().inflate(R.menu.main, menu);

if (mIgate.getIGateState()==iGateHostState.iGateHostStateSearching) {

menu.findItem(R.id.scanning_start).setVisible(false);

menu.findItem(R.id.scanning_stop).setVisible(true);

menu.findItem(R.id.scanning_indicator)

.setActionView(R.layout.progress_indicator);

} else {

menu.findItem(R.id.scanning_start).setVisible(true);

menu.findItem(R.id.scanning_stop).setVisible(false);

menu.findItem(R.id.scanning_indicator).setActionView(null);

}

return true;

}

@Override

public boolean onOptionsItemSelected(MenuItem item) {

switch (item.getItemId()) {

case R.id.scanning_start:

mIgate.startScanning(false);

break;

case R.id.scanning_stop:

mIgate.stopScanning();

break;

}

invalidateOptionsMenu();

return true;

}

@Override

public void onBackPressed(){

for(int i=0;i

mIgate.iGateDeviceDisconnect(mConnectedBluetoothDevicesAddress.get(i));

}

if(mIgate.getIGateState()==iGateCallBacks.iGateHostState.iGateHostStateSearching){

mIgate.stopScanning();

}

super.onBackPressed();

}

public void iGateHostDidUpdateState(final iGateHostState state){

switch(state){

case iGateHostStatePoweredOff:

Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);

startActivityForResult(enableBtIntent, ENABLE_BT_REQUEST_ID);

case iGateHostStateIdle:

case iGateHostStateSearching:

invalidateOptionsMenu();

break;

default:

break;

}

// adding to the UI have to happen in UI thread

runOnUiThread(new Runnable() {

@Override

public void run() {

switch(mIgate.getIGateState()){

case iGateHostStateIdle:

if(mConnectedBluetoothDevicesAddress.size()==0){

mIGateState.setText(R.string.str_idle);

}

else{

listConnectedDevices();

}

break;

case iGateHostStateSearching:

mIGateState.setText(R.string.str_scanning);

break;

default:

break;

}

}

});

}

public void iGateDeviceFound(final String bluetoothAddress, final int rssi, final byte[] record){

runOnUiThread(new Runnable() {

@Override

public void run() {

mIGateState.setText(getString(R.string.str_found) bluetoothAddress);

mIgate.stopScanning();

mIgate.iGateDeviceConnect(bluetoothAddress);

}

});

}

public void iGateDeviceConnected(final String bluetoothAddress){

if(!mConnectedBluetoothDevicesAddress.contains(bluetoothAddress)){

mConnectedBluetoothDevicesAddress.add(bluetoothAddress);

}

runOnUiThread(new Runnable() {

@Override

public void run() {

mIgate.iGateDeviceBondService(bluetoothAddress);

}

});

}

public void iGateDeviceServiceBonded(final String bluetoothAddress){

if(!mConnectedBluetoothDevicesAddress.contains(bluetoothAddress)){

mConnectedBluetoothDevicesAddress.add(bluetoothAddress);

}

runOnUiThread(new Runnable() {

@Override

public void run() {

listConnectedDevices();

}

});

}

public void iGateDeviceDisConnected(final String bluetoothAddress){

mConnectedBluetoothDevicesAddress.remove(bluetoothAddress);

runOnUiThread(new Runnable() {

@Override

public void run() {

if(mConnectedBluetoothDevicesAddress.size()>0){

String allConnectedDevices=new String();

for(int i=0;i

allConnectedDevices =" " mConnectedBluetoothDevicesAddress.get(i);

}

mIGateState.setText(getString(R.string.str_connected) allConnectedDevices);

}

else{

mIGateState.setText(getString(R.string.str_idle));

}

}

});

}

public void iGateDeviceReceivedData(final String bluetoothAddress, final byte[] data){

try {

final String tmpValue = new String(data,"UTF-8");

runOnUiThread(new Runnable() {

@Override

public void run() {

mReceivedData.append(tmpValue);

Log.e("receivedata", "receivedata");

}

});

} catch (UnsupportedEncodingException e) {

e.printStackTrace();

}

}

public void iGateDeviceUpdateRssi(final String bluetoothAddress, final int rssi){

Log.i("------", String.format("Rssi report %s,%d", mIgate.iGateDeviceGetName(bluetoothAddress),rssi));

}

public void iGateDeviceLinkLossAlertLevelReport(final String bluetoothAddress, byte alertLevel){

Log.i("------", String.format("Link loss alert level %s,%d", bluetoothAddress,alertLevel));

}

public void iGateDeviceTxPowerReport(final String bluetoothAddress, byte txPower){

Log.i("------", String.format("Tx Power %s,%d", bluetoothAddress,txPower));

}

private void listConnectedDevices(){

String allConnectedDevices=new String();

for(int i=0;i

allConnectedDevices =" " mConnectedBluetoothDevicesAddress.get(i);

}

mIGateState.setText(getString(R.string.str_connected) allConnectedDevices);

}

private iGate mIgate=null;

}



推荐阅读
author-avatar
熊孝琳_506
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有