基于netty框架的JTT808/JTT905/JTT1078协议客户端
JTT808客户端网络处理
private void connectServer() {
try {
group = new NioEventLoopGroup();
bootstrap = new Bootstrap()
.option(ChannelOption.SO_KEEPALIVE, true)
.option(ChannelOption.RCVBUF_ALLOCATOR, new FixedRecvByteBufAllocator(65535))
.channel(NioSocketChannel.class)
.group(group)
.handler(new ChannelInitializer<SocketChannel>() {
ByteBuf delimiter = Unpooled.buffer(1);
@Override
protected void initChannel(SocketChannel socketChannel) throws Exception {
delimiter.writeByte(0x7E);
ChannelPipeline pipeline = socketChannel.pipeline();
pipeline.addLast(new DelimiterBasedFrameDecoder(65535, delimiter));
//写空闲30秒发送心跳
pipeline.addLast(new IdleStateHandler(30, HEART_TIME, 0));
pipeline.addLast(new JTT808Decoder());
pipeline.addLast(new JTT808Encoder());
pipeline.addLast(new JTT808Handler(JTT808Client.this, listener));
}
});
ChannelFuture channelFuture = bootstrap.connect(new InetSocketAddress(ip, port));
channelFuture.addListener(channelFutureListener);
} catch (Exception e) {
e.printStackTrace();
if (listener != null) {
listener.onConnectionSateChange(OnConnectionListener.DIS_CONNECT);
}
}
}
JTT808消息处理
private void handData(JTT808Bean bean, Channel channel) throws Exception {
int msgId = ByteUtil.bytes2Int(bean.getMsgHeader().getMsgId());
Log.d(TAG, "msgId:" + Integer.toHexString(msgId));
switch (msgId) {
//注册应答
case 0x8100:
registerResult(bean);
break;
//平台通用应答
case 0x8001:
universalResult(bean);
break;
//1078,回放列表
case 0x9205:
localVideoList(bean);
break;
case 0x9202:
PlayBackVideoAudioLiveControl(bean);
break;
case 0x9201:
upLocalVideo(bean);
break;
//jtt808,设置终端参数
case 0x8103:
terminalParams(bean);
break;
case 0x8106:
JT808_0x0104(bean);
break;
//查询终端属性
case 0x8107:
queryAtrribute();
break;
//terminal update
case 0x8108:
update(bean);
break;
//jtt1078,实时音视频传输请求
case 0x9101:
audioVideoLive(bean);
break;
//jtt1078,音视频实时传输控制
case 0x9102:
audioVideoLiveControl(bean);
break;
//jtt1078,音视频实传输状态通知
case 0x9105:
audioVideoLiveState(bean);
break;
case 0x9206:
//上传文件指令
uploadFiles(bean);
break;
case 0x1206:
//上传文件指令
break;
case 0x9003:
transferMediaInfo(bean);
break;
case 0x8300:
textdelivery(bean);
break;
default:
Log.d(TAG, "msgid:" + Integer.toHexString(msgId));
break;
}
}
JTT905客户端网络处理
private void connectServer() {
try {
group = new NioEventLoopGroup();
bootstrap = new Bootstrap()
.option(ChannelOption.SO_KEEPALIVE, true)
.option(ChannelOption.RCVBUF_ALLOCATOR, new FixedRecvByteBufAllocator(65535))
.channel(NioSocketChannel.class)
.group(group)
.handler(new ChannelInitializer<SocketChannel>() {
ByteBuf delimiter = Unpooled.buffer(1);
@Override
protected void initChannel(SocketChannel socketChannel) throws Exception {
delimiter.writeByte(0x7E);
ChannelPipeline pipeline = socketChannel.pipeline();
pipeline.addLast(new DelimiterBasedFrameDecoder(65535, delimiter));
//写空闲30秒发送心跳
pipeline.addLast(new IdleStateHandler(30, HEART_TIME, 0));
pipeline.addLast(new JTT905Decoder());
pipeline.addLast(new JTT808Encoder());
pipeline.addLast(new JTT905Handler(JTT905Client.this, listener));
}
});
ChannelFuture channelFuture = bootstrap.connect(new InetSocketAddress(ip, port));
channelFuture.addListener(channelFutureListener);
Log.e("JTT905","start connect");
} catch (Exception e) {
e.printStackTrace();
if (listener != null) {
listener.on905ConnectionSateChange(OnConnectionListener.DIS_CONNECT);
}
}
}
JTT905客户端消息处理
/**
* 处理事件
*
* @param bean
*/
private void handData(JTT905Bean bean, Channel channel) throws Exception {
int msgId = ByteUtil.bytes2Int(bean.getMsgHeader().getMsgId());
Log.d(TAG, "msgId:" + Integer.toHexString(msgId));
switch (msgId) {
//注册应答
case 0x8100:
registerResult(bean);
break;
//平台通用应答
case 0x8001:
universalResult(bean);
break;
//回放列表
case 0x9205:
localVideoList(bean);
break;
case 0x9202:
PlayBackVideoAudioLiveControl(bean);
break;
case 0x9201:
upLocalVideo(bean);
break;
//jtt808,设置终端参数
case 0x8103:
terminalParams(bean);
break;
case 0x8104:
JT905_0x0104(bean);
break;
//查询终端属性
case 0x8107:
queryAtrribute();
break;
//terminal update
case 0x8108:
update(bean);
break;
//jtt1078,实时音视频传输请求
case 0x9101:
audioVideoLive(bean);
break;
//jtt1078,音视频实时传输控制
case 0x9102:
audioVideoLiveControl(bean);
break;
//jtt1078,音视频实传输状态通知
case 0x9105:
audioVideoLiveState(bean);
break;
case 0x9206:
//上传文件指令
uploadFiles(bean);
break;
case 0x1206:
//上传文件指令
break;
case 0x9003:
transferMediaInfo(bean);
break;
case 0x8300:
textdelivery(bean);
break;
default:
Log.d(TAG, "msgid:" + Integer.toHexString(msgId));
break;
}
}
定位
private void startReceivingLocationUpdates() {
if (locationManager == null) {
locationManager = (android.location.LocationManager)
context.getSystemService(Context.LOCATION_SERVICE);
}
if (locationManager != null) {
try {
locationManager.requestLocationUpdates(
android.location.LocationManager.NETWORK_PROVIDER,
1000,
0.1F,
mLocationListeners[1]);
} catch (SecurityException ex) {
Log.i(TAG, "fail to request location update, ignore", ex);
} catch (IllegalArgumentException ex) {
Log.d(TAG, "provider does not exist " + ex.getMessage());
}
try {
locationManager.requestLocationUpdates(
android.location.LocationManager.GPS_PROVIDER,
1000,
0.1F,
mLocationListeners[0]);
} catch (SecurityException ex) {
Log.i(TAG, "fail to request location update, ignore", ex);
} catch (IllegalArgumentException ex) {
Log.d(TAG, "provider does not exist " + ex.getMessage());
}
Log.d(TAG, "startReceivingLocationUpdates");
}
}
服务器实时定位截图
实时视频截图
如果需要了解具体的功能实现,QQ:409259564