3.系统运行库
1)程序库
Android 包含一些C/C++库,这些库能被Android系统中不同的组件使用。它们通过 Android 应用程序框架为开发者提供服务。以下是一些核心库:
* 系统 C 库 - 一个从 BSD 继承来的标准 C 系统函数库( libc ), 它是专门为基于 embedded linux 的设备定制的。
* 媒体库 - 基于 PacketVideo OpenCORE;该库支持多种常用的音频、视频格式回放和录制,同时支持静态图像文件。编码格式包括MPEG4, H.264, MP3, AAC, AMR, JPG, PNG 。
* Surface Manager - 对显示子系统的管理,并且为多个应用程序提 供了2D和3D图层的无缝融合。
* LibWebCore - 一个最新的web浏览器引擎用,支持Android浏览器和一个可嵌入的web视图。
* SGL - 底层的2D图形引擎
* 3D libraries - 基于OpenGL ES 1.0 APIs实现;该库可以使用硬件 3D加速(如果可用)或者使用高度优化的3D软加速。
* FreeType -位图(bitmap)和矢量(vector)字体显示。
* SQLite - 一个对于所有应用程序可用,功能强劲的轻型关系型数据库引擎。
2)Android 运行库
Android 包括了一个核心库,该核心库提供了JAVA编程语言核心库的大多数功能。
每一个Android应用程序都在它自己的进程中运行,都拥有一个独立的Dalvik虚拟机实例。Dalvik被设计成一个设备可以同时高效地运行多个虚拟系统。 Dalvik虚拟机执行(.dex)的Dalvik可执行文件,该格式文件针对小内存使用做了优化。同时虚拟机是基于寄存器的,所有的类都经由JAVA编译器编译,然后通过SDK中 的 “dx” 工具转化成.dex格式由虚拟机执行。
Dalvik虚拟机依赖于linux内核的一些功能,比如线程机制和底层内存管理机制。
只需在AndroidManifest.xml中对activity加入android:theme="@android:style/Theme.Dialog"
例如:
<activity android:name="OtherActivity" android:label="@layout/other"
android:theme="@android:style/Theme.Dialog"></activity>这篇文章分为两部分:
第一部分:使用系统自身带的蓝牙功能,直接用intent调用就可以
private void sendFile(FileInfo fileInfo){
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
//这个类型函数是自己工具类的方法,你可以自己设置文件类型,例如图片文件:image/*
//不想写类型直接*/*也是可以的
String type = UtilFileClassify.getMIMEType(fileInfo.fileName);
intent.setType(type);
//这里setClassName就是指定蓝牙,不写这句就弹出选择用什么发送
//有蓝牙啊,gmail啊,彩信之类的
intent.setClassName("com.android.bluetooth"
, "com.android.bluetooth.opp.BluetoothOppLauncherActivity");
intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(
new File(fileInfo.filePath)));
startActivity(intent);
}
需注意的是android系统好像不愿意你蓝牙传送apk,如果你传送会显示失败。
在网上看到传送方式有两种,我去试验下那个蓝牙用stream方式可不可以传送apk
第二部分:用BluetoothAdapter那一系列的操作自己写server 以及 client
1.获取本机的蓝牙适配器:
BluetoothAdapter btAdapter = BluetoothAdapter.getDefaultAdapter();//获取本机蓝牙适配器
if(btAdapter == null){
Log.e("","there isnt any blouetooth in your device!");
return;
}
2.打开蓝牙适配器:
if(!btAdapter.isEnable){
Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivity(intent);
}
3.获取已经配对的蓝牙设备:
Set<BluetoothDevice> pairedDevices = btAdapter.getBondedDevice();
for(int i = o;i<pairedDevice.size();i++){
BluetoothDevice btDevice = pairedDevice.get(i);
String str = "Name:"+btDevice.getName()+" Address:"+btDevice.getAddress();
Log.e("",str);
}
4.打开可可检测性:
Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE); intent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300); startActivity(intent);
注意这里的时间最多设置为300s 当大于300S时候还一样是300s
5.扫描周围的设备:
if(btAdapter.isDiscovering){
btAdapter.cancelDiscovery();
}
btAdapter.startDiscovery();//就这样启动就可以,当扫描到设备后系统会发广播,所以要注册广播来接收扫描到的设备信息
private BrodcastReceiver btReceiver = new BroadcastReceiver(){
public void onReceive(Context context,Intent intent){
String action = intent.getAction();
if(BlutoothDevice.ACTION_FOUND.equals(action)){
BluetoothDevice btDevice = intent.getParcelableExtra(
BluetoothDevice.EXTRA_DEVICE);
Log.e("",""+btDevice);
} else if(BluetoothDevice.ACTION_DISCOVERY_FINISHED.equals(action)){
Log.e("","discovery finished");
}
}
}
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
this.registerReceiver(btReceiver,filter);
filter = new IntentFilter(BluetoothDevice.ACTION_DISCOVERY_FINISHED);
this.registerReceiver(btReceiver,filter);
//别忘记在程序退出时候取消广播
public void onDestroy(){
super.onDestroy();
if(btAdapter != null){
btAdapter.cancelDiscovery;
}
this.unregisterReceiver(btReceiver);
}
6.连接,这个主要是BluetoothSocket和BluetoothServerSocket,客户端,和服务器
其实这个UUID我也是一知半解,百科看完了大概就当个ID用吧,服务端跟客户端要一样的
网上有一篇很不错的帖子讲了这部分的原理:
[url]
http://lighthearts.blog.163.com/blog/static/1726840522011791111499/
[/url]
那我就借用下他的两小块代码吧:
//服务端
private BluetoothServerSocket serverSocket = btAdapter
.listenUsingRfcommWithServiceRecord(SERVICE_NAME,SERVICE_UUID);
private BluetoothSocket exchangeSocket = serverSocket.accept();
//客户端
private BluetoothSocket clientSocket = btDevice
.createRfcommSocketToServerRecord(SERVER_UUID);
clientSocket.connet();
//接下来就是clientSocket.getOutputStream().write(byte[]);